One approach is to pick a different delimiter besides /
. For example, using |
:
sed -ie "s|queue_directory = /var/spool/postfix-secondary|queue_directory = /var/spool/postfix-$newnumber|g" /etc/postfix-$newnumber/main.cf
Another approach is to backslash-escape your other slashes:
sed -ie "s/queue_directory = \/var\/spool\/postfix-secondary/queue_directory = \/var\/spool\/postfix-$newnumber/g" /etc/postfix-$newnumber/main.cf
Two more suggestions for your particular usage. First, I don't think you need the g flag, unless you anticipate the substitution appearing multiple times in the same line. Second, if you are just trying to change a directive, you could potentially just change it no matter what the previous value. For example:
sed -ie "s|^queue_directory =.*|queue_directory = /var/spool/postfix-$newnumber|" /etc/postfix-$newnumber/main.cf
s|queue_directory = /var/spool.*|newnumber|
.s///
the stuff between those comprise the address and replace fields. If the stuff contains/
then you just have to use something else likes|||
, or backslash escape the stuff within likes/\///
. It works for regular addresses too, but those always require a backslash when not/
like:sed '\|^/|s/.//'
which will remove a/
if it occurs at the head of a line. And so the full command is your command wheres///
iss|||
instead.