Linux – Sed replace string with variable

bashlinuxsed

Im trying to replace string with variable with this command but not works

sed '/AXG/s/;/${HOMEx}/g'    

Please help me with this

Best Answer

Get rid of the single quotes. They prevent variable expansion.

sed "/AXG/s/;/${HOMEx}/g"

If HOMEx="test" this becomes: sed "/AXG/s/;/test/g"

Compare https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.htm and https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html

Beware though: if the variable HOMEx contains something sed recognises you'll get an error. In this case the main things would be a backreference like "\1" or forward slash "/".

Related Question