0

I have file <temp_test.sh> in which I have defined a variable Min_Inc=300000.

I then want to get the user input to change the value of Min_Inc. I followed these steps.

  1. Read the value using echo $Min_Inc and displayed it.
  2. Sought User input for the new value and read it with read New_Val.
  3. Tried replacing Min_Inc=300000 with Min_Inc=$New_Val using sed as sed 's/Min_Inc=300000/Min_Inc=$New_Val/'

This gives an error message. Moreover, I want to make this change permanent which I can make by redirecting the output to another file.

terdon
  • 100,812
Ashok
  • 11

1 Answers1

1

Variables aren't expanded inside single quotes. You either need to use double quotes:

sed -i "s/Min_Inc=300000/Min_Inc=$New_Val/" /path/file-name

Or break the quotes for the variable:

sed -i 's/Min_Inc=300000/Min_Inc='$New_Val'/' /path/file-name

The -i flag writes the value back to the file, you can even make a backup of the file that way by adding a string to the -i flag. For example -i.bak will create a backup file with the extension .bak.

terdon
  • 100,812
Videonauth
  • 33,355
  • 17
  • 105
  • 120