Search code examples
bashsedreplaceslash

passing sed argument with slash ( making a directory a string ?)


I am trying to pass in assets/css/images to replace images in multiple files.

So I wrote this script

$list
$word
$neword
$i

echo "select files: "
read list
echo "input word to search for: "
read word
echo "input word to replace with: "
read neword

for i in $list
do
sed -i "s/${word}/${neword}/g" $i

done

It works for words without a /, and I can see why, but I don't know how to get around it. Anyone?


Solution

  • You should do like this to escape / in sed

    Try Following,

    sed -i "s#${word}#${neword}#g" "$i"
    

    or

    sed -i "s~${word}~${neword}~g" "$i"
    

    Edit: Added Quotes to Filename as Suggested by @tripleee for avoiding condition like filename contains in $i have spaces.