1

How to replace a string in a file by bash variable, please

I tried

  i="1"
  sed -i 's/%q/0$i/g' file
  sed -i 's/%q/0"$i"/g' file

There is 0$i or 0"$i" in the file instead of 01. Thank you

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Elena Greg
  • 1,061
  • 1
  • 11
  • 26

1 Answers1

1

Change your script as follow:

i="1"
sed -i "s/%q/0$i/g" file
sed -i "s/%q/0\"$i\"/g" file

and remember: the shell variables substitution does not happen inside single quotes. ;)

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74