2

I 've got a file named file.conf containing:

this is the configuration text and this is the WORD to change.

Running:

sed -i 's/WORD/"ONE TWO"/g' file.conf 

I will have file.conf modified:

this is the configuration text and this is the "ONE TWO" to change.

now if I make a script, using read:

read -p 'word to change' TEXT -> "ONE TWO"
echo $TEXT -> "ONE TWO" 
sed -i 's/WORD/'$TEXT'/g' file.conf

it does not work with error message:

sed: -e expression #1, char 11: unterminated `s' command

file.conf is not modified in this case.

but it works if I read $TEXT with only one word without spaces: "ONE" for instance.

Thanx folks.

achille
  • 173
  • 1
  • 3
  • 12
  • 2
    Possible duplicate of [How to use variables in a command in sed?](https://stackoverflow.com/questions/19151954/how-to-use-variables-in-a-command-in-sed) – oguz ismail Nov 29 '18 at 18:14

1 Answers1

1

Double quote variable like this:

sed -i 's/WORD/'"$TEXT"'/g' file.conf

Even safer:

sed -i 's/WORD/'"${TEXT}"'/g' file.conf
Kubator
  • 1,373
  • 4
  • 13