Luca Luca - 1 year ago 67
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download