How to use sed to remove variables value from a string [duplicate]
By Sebastian Wright
I am writing a script in bash and I can remove a string from a sentence using the code below:
echo 'This is a foo test' | sed 's/\<foo\>//g'OUTPUT: 'This is a test'
However, I want to be able to replace the foo with a variable. I tried the command below but it does not remove the values from the sentence.
temp="foo"
echo 'This is a foo test' | sed 's/\<$temp\>//g'0OUTPUT: 'This is a foo test'
1 Answer
Within single quotes the variable is not substituted (because $ has no special meaning withing single quotes). You can use double quotes instead:
echo 'This is a foo test' | sed "s/\<$temp\>//g"