0

I'm trying to write a bash script to configure a server and I need to change the line:

    listen = /var/run/php5-fpm.sock

to equal the following:

    listen = 127.0.0.1:9000

in the file:

    /etc/php5/fpm/pool.d/www.conf

So I've been looking at tutorials for using sed and I've tried the following command to no avail:

    $~: sed -i 's//var/run/php5-fpm.sock/127.0.0.1:9000/g' /etc/php5/fpm/pool.d/www.conf
    $~: sed: -e expression #1, char 8: unknown option to `s'

I've tried escaping the forward slash with a backslash: '/' but I think I'm on the wrong track. There must be a better way to do this?

Thanks for your help.

1 Answer 1

3

This is because you are trying to replace the character '/' in the pattern, and this character is used to delimit the 's///' expression. You have two choices, you can escape every '/' character with '/'or - and this is the one I prefer, use a different character to delimit the pattern and replacement string - I tend to use '!'

The character immediately after the 's' is used to delimit the expressions.

sed -i 's!/var/run/php5-fpm.sock!127.0.0.1:9000!g' /etc/php5/fpm/pool.d/www.conf

I actually have got into the habit of ALWAYS using '!' for sed, and perl - as you end up having to escape less characters and ultimately save time.

Sign up to request clarification or add additional context in comments.

1 Comment

Beware: ! is a metacharacter in bash, an infestation from the world of C shells. It is fine inside single quotes (AFAIK), but can cause problems in double quotes or outside quotes. Regexes should almost always be single-quoted, so it isn't a huge issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.