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
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
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 "/".
A better way for literal strings with forward slashes:
In circumstances where the replacement string or pattern string contain slashes, you can make use of the fact that GNU sed allows an alternative delimiter for the substitute command. Common choices for the delimiter are the pipe character | or the hash # - the best choice of delimiting character will often depend on the type of file being processed. In your case you can try
The character 'g' after last # is used to change all occurrences in file if you need to change only first occurrence then remove the 'g'.