0

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: `$'

potame
  • 7,597
  • 4
  • 26
  • 33
Luca
  • 41
  • 4
  • 2
    Possible duplicate of [How to use variables in a command in sed?](http://stackoverflow.com/questions/19151954/how-to-use-variables-in-a-command-in-sed), or [shell variables in sed script](http://stackoverflow.com/q/7006910/3266847), and [this](http://stackoverflow.com/questions/26319607/bash-using-sed-with-a-variable-line-number-and-variables-in-the-line-replaceme), and [here](http://unix.stackexchange.com/questions/177167/how-to-use-variables-in-sed-command)... – Benjamin W. Oct 18 '16 at 14:45

2 Answers2

0

drop the quotes for double quotes so environment variables are evaluated:

sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

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
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • with this syntax doesn't work i've this error: sed: -e expression #1, char 6: unknown command: `4' – Luca Oct 18 '16 at 14:55