1

I am confused a little bit. I would like to swap 2 directory in the apache2 config files with the sed program.

APACHE_CONF=/etc/apache2/apache2.conf
FULL_DIR=/home/$USER/new
DEFAULT_DIR=/var/www/html`

This works fine:

sudo sed -i.bak 's|'$DEFAULT_DIR'|'$FULL_DIR'|' "$APACHE_CONF"

These are not (these don't have error, just do nothing):

sudo sed -i.bak 's/"$DEFAULT_DIR"/"$FULL_DIR"/' "$APACHE_CONF"
sudo sed -i.bak 's/\"$DEFAULT_DIR"/\"$FULL_DIR"/' "$APACHE_CONF"

Can anybody explain why don't these work and why does the first one work?

muru
  • 197,895
  • 55
  • 485
  • 740
ampika
  • 133
  • Quoting prevents the shell from treating characters as special - it does not help in preventing sed itself from treating / as special – steeldriver May 11 '15 at 15:33
  • That is right. I used backslash after forward slash to sign to the interpreter forward slash is the delimiter. You need to use this: | as a delimiter. According this. – ampika May 11 '15 at 15:46
  • 1
    You can use / as the sed delimiter only if you escape every / in the pattern and replacement e.g. define DEFAULT_DIR=\/var\/www\/html and so on. Putting a backslash after the / like s/\"$DEFAULT_DIR"/ is not the same thing (in fact it just makes sed treat the first " as literal, so that it no longer matches the text in your file). – steeldriver May 11 '15 at 16:11

0 Answers0