Search code examples
bashsedquoting

Sed not working inside bash for loop


I want to replace an image path inside all html files recursively in all folders.

#!/bin/bash

for image in images/*.png
do

  echo "sed -i .bak -e 's|$image|image2/$image|g' app/www/*.html"
  sed -i .bak -e 's|$image|image2/$image|g' app/www/*.html

done

The weird thing is that when I manually execute the command:

  sed -i .bak -e 's|images/add.png|image2/add.png|g' app/www/*.html

everything works fine and the path is replaces. However inside the for loop this doesn't work while the echo part echoes the same line as mentioned above.


Solution

  • You need to use " and not ' Otherwise the $image is not evaluated

    Edit: it works in the echo because the ' surrounded string is surrounded by " ;)