Bash – sed replace command inside of a bash script


I have a problem replacing a command inside of a script, the offending line in the script looks like this:

mail -s "$(hostname) on $(date)"

It should be replaced with a line like this:

nail -r "" -s "Subject" -S smtp=

But I can't get sed to do a replacement 🙂 I wrote a small script for that purpose:


old="mail -s \"\$(hostname) on \$(date)"
new="nail -r \"\" -s \"Subject\" -S smtp="

sed -i 's|$old|$new|'

Does anyone have any advice?

Best Solution

sed -i "s|$old|$new|"

Note the double quotes.