3

I want to use my Centos VM's $HOSTNAME inside a call to sed like so:

sed -i 's/Apache 2 Test Page/$HOSTNAME Test Page/g' /var/www/error/noindex.html

But this just replaces Apache 2 Test Page with $HOSTNAME Test Page... I know I can do it manually, but I've got a good reason for wishing to do it this way.


FWIW, I'm actually doing this inside a bash script that gets called from a Vagrantfile, which in turn provisions multiple VM's (each with Apache2 installed), so that I can test-out the load-balancing capabilities of HAProxy. so I just want a simple way to differentiate between my 3 web-servers, and II figure that modifying the default Apache page is the easiest way to do that.

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
  • 2
    Enclose sed's(`s///`) with double quotes instead single quotes for variable expansion. – Avinash Raj Jun 11 '14 at 16:38
  • @AvinashRaj Yes, this would be ok here too. I prefer the solution I've posted, because then I don't have to worry about escaping shell characters in the sed command. (Which aren't a problem in this case) – hek2mgl Jun 11 '14 at 16:40

1 Answers1

9

Enclose sed's s/// with double quotes instead of single quotes for variable expansion.

sed -i "s/Apache 2 Test Page/$HOSTNAME Test Page/g" /var/www/error/noindex.html

Example:

$ echo 'foo Apache 2 Test Page bar' | sed "s/Apache 2 Test Page/$HOSTNAME Test Page/g"
foo avinash-Lenovo-IdeaPad-Z500 Test Page bar
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274