f10bit
f10bit

Reputation: 19850

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 "[email protected]" -s "Subject" -S smtp=255.255.255.255

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

#!/bin/bash

old="mail -s \"\$(hostname) on \$(date)"
new="nail -r \"[email protected]\" -s \"Subject\" -S smtp=255.255.255.255"

sed -i 's|$old|$new|' script.sh

Does anyone have any advice?

Upvotes: 0

Views: 5261

Answers (3)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143319

sed -i "s|$old|$new|" script.sh

Note the double quotes.

Upvotes: 3

ennuikiller
ennuikiller

Reputation: 46985

Sed by default does not do in Place editing. If you are using gnu Sed try providing the in place flag -ikbak

Upvotes: 1

Related Questions