sed substitution with Bash variables sed substitution with Bash variables bash bash

sed substitution with Bash variables


Variables inside ' don't get substituted in Bash. To get string substitution (or interpolation, if you're familiar with Perl) you would need to change it to use double quotes " instead of the single quotes:

# Enclose the entire expression in double quotes$ sed "s/draw($prev_number;n_)/draw($number;n_)/g" file.txt > tmp# Or, concatenate strings with only variables inside double quotes# This would restrict expansion to the relevant portion# and prevent accidental expansion for !, backticks, etc.$ sed 's/draw('"$prev_number"';n_)/draw('"$number"';n_)/g' file.txt > tmp# A variable cannot contain arbitrary characters# See link in the further reading section for details$ a='foobar'$ echo 'baz' | sed 's/baz/'"$a"'/g'sed: -e expression #1, char 9: unterminated `s' command

Further Reading:


Variables within single quotes are not expanded, but within double quotes they are. Use double quotes in this case.

sed "s/draw($prev_number;n_)/draw($number;n_)/g" file.txt > tmp

You could also make it work with eval, but don’t do that!!


This may help:

sed "s/draw($prev_number;n_)/draw($number;n_)/g"