SUMMARY: Using variables in sed

From: Cohen, Laurence <>
Date: Wed Jan 04 2006 - 09:49:31 EST
Thank you to the many of you that responded so quickly!  The answer was
to use double quotes around the sed script rather than single quotes, as
in the following.

Sed -e "s/HOST/$HOST/g" > filename

Very simple but so elusive!

Thanks again!

-----Original Message-----
[] On Behalf Of Cohen,
Sent: Wednesday, January 04, 2006 9:42 AM
Subject: Using variables in sed

Hello everyone!

Would anyone have a hint as to how to use a variable from the OS in sed?
For example, I want to be able to substitue the hostname of a system for
every instance of the word HOST in a file.  I set the variable
$HOST=`hostname` and then run sed -e 's/HOST/$HOST/g' > filename.  The
result of this is that it substitutes the string "$HOST" instead of it's
value.  I even tried sed -e 's/HOST/`echo $HOST`/g' > filename, and the
result is that it substitutes the string "`echo $HOST`, which is also
not what I want.  How do I get sed to understand that I'm referring to a
variable and not a string?


Laurence H. Cohen
ISM Unix System Administrator
sunmanagers mailing list
sunmanagers mailing list
Received on Wed Jan 4 09:50:12 2006

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:54 EST