Luca Luca - 5 days ago 3x
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: `$'


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


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