Luca Luca - 1 month ago 11
Linux Question

Insert variable in specific place using sed -i

i would insert two variable in sed command:

sed -i '39,41 s/^#//' file


i would

sed -i '$LINE,$LINE_INCREMENTED s/^#//' file


but return this:

sed: -e expression #1, char 9: unknown command: `$'

Answer

Shell variables are not expanded when put inside single quotes, they are treated literally then.

Do:

sed -i "$LINE,$LINE_INCREMENTED"' s/^#//' file

Assuming the variables only contain digits.

As s/^#// part does not contain any shell expansion, putting double quotes over the full expression would do too, better readability:

sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
Comments