How to use variables in a sed command

Posted on November 29, 2021 at 1:03 pm

Use double quotes to make the shell expand variables and preserve whitespace:

sed -i "s/$var1/abc/g" "$file"

If you have a slash / in the variable, use different separator:

sed -i "s|$var1|abc|g" "$file"

Receive updates via email

Other Posts

Updated Posts