Bash script - pass command line arg to embedded sed script with multiple args
What I'm trying to do is pretty simple. Getting it to work is turning out not
to be. What I want to do is call a bash script with a couple of arguments,
and, within the script, call sed to use those args to replace two placeholders
in a file:
bashscript SUB1 SUB2
This line inside bashscript doesn't work:
sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' < input > output
It doesn't work because the command line args ($1, $2) are quoted and don't
get replaced. However, because I'm using the -e argument to sed, I have to
quote the argument or sed gets all huffy. I've tried all sorts of variations
on the quoting and either bash or sed isn't happy no matter what I do.
The only workaround I've found is to do the substitutions in two passes:
sed s/SUB1/$1/ < input > temp
sed s/SUB2/$2/ < temp > output
Since I'm not using multiple argument to sed, I don't have to quote the arg.
That's an ugly hack and really rubs me the wrong way, however, and it'll get
really ugly if I end up having to do more than two substitutions, which I
expect to do at a later date. I suppose I could pipe one sed command to
another rather than using a temp file but that's not significantly more
palatable.
Any ideas on a cleaner approach on how to do this, either by getting this
right or using an alternative method, are welcome. One thing to note is that
this is a small part of a larger, more complex script, so the bash script
can't (easily) go away. I don't have to use sed to do the replacement if
there's another approach from within the bash script that would work.
--
"Americans detest all lies except lies spoken in public or printed lies." -
Edgar Watson Howe
Reply to: