Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to replace $fileWithPath in $file, however this not works because (I think) path is not escaped. how to escape it?

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

share|improve this question

2 Answers 2

up vote 17 down vote accepted
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 "/".

share|improve this answer
Beware that if $fileWithPath contains whitespace, the wrong pattern is inserted. –  Lekensteyn Nov 7 '11 at 18:09

A better way for literal strings with forward slashes:

sed -i "s|my/path|my/other/path|g" myFileOfPaths.txt
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.