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?
sed
itself from treating/
as special – steeldriver May 11 '15 at 15:33/
as thesed
delimiter only if you escape every/
in the pattern and replacement e.g. defineDEFAULT_DIR=\/var\/www\/html
and so on. Putting a backslash after the/
likes/\"$DEFAULT_DIR"/
is not the same thing (in fact it just makessed
treat the first"
as literal, so that it no longer matches the text in your file). – steeldriver May 11 '15 at 16:11