0

I am trying to do this script to work:

#!/bin/bash
win_location=$(cat /boot/grub/grub.cfg | grep 'class windows' | cut -d "'" -f2)
sudo sed -i 's/^'GRUB_DEFAULT='.*$/GRUB_DEFAULT="$win_location"/' /etc/default/grub
sudo update-grub2

But the problem is it does not recognizes the variable, which should be printed between quotes to the file because its value include spaces.

Any idea?

Thank you in advance

Diego
  • 1

1 Answers1

0

If you want to use variables into the sed, please use double quotes instead of single quotes

As per your example, you have variables in sed

sudo sed -i "s|^GRUB_DEFAULT=.*$|GRUB_DEFAULT=\"$win_location\"|g" /etc/default/grub
  • Using double quotes around the sed expression has nothing to do with its use in a script. If you want to expand shell variables in the expression, then you need double quotes instead of single quotes. Also, a single quoted string can never contain a single quote, which is another reason for using double quotes. The delimiter does not need to be a pipe character. (almost) Any character would do fine. An explanation of why / may be a problematic character to use would be in order. – Kusalananda Oct 15 '20 at 09:18
  • I agree with you, my theory was wrong, apart of that the above is working. – Claudius D. Oct 15 '20 at 09:26
  • 1
    So change your answer to accommodate for the comments you've received, as it is still a wrong answer without the changes. – nethero Oct 15 '20 at 10:22