I try the fallowing code and it does nothing
var1=QQ
sed -i 's/$var1/ZZ/g' $file
However this code replaces QQ with ZZ
sed -i 's/QQ/ZZ/g' $file
how to use variables in SED?
|
|
The shell is responsible for expanding variables. If you use single quotes for strings, the contents will be treated literally, so Using double quotesUse double quotes to make the shell expand variables while keeping whitespace:
Beware: if you need to put backslashes in your replacement (e.g. for back references), you need double slashes (
Using single quotesIf you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:
Notice how I use ExampleThe shell then runs the command
If
|
||||
|
|