Ubuntu – How to escape file path in SED


I would like to replace $fileWithPath in $file, however this doesn't work because (I think) path is not escaped. How to escape it?

sed -i 's/${fileWithPath}/HAHA/g' $file

Best Answer

  • sed -i 's|'$fileWithPath'|HAHA|g' file

    Single quotes define a string literal. Putting the variable outside the literal allows the shell to expand that part.

    Also: if you are going to parse paths, use a delimiter in the sed command that doesn't confuse with the directory delimiter "/".

  • Related Question