I'm just trying to replace a setting in a php.ini file using a bash script.

This is my case:

I have a very specific setting and the default is:

;date.timezone =

Now, I'm trying to replace this by sed to this:

date.timezone = "America/Mexico_City"

And the command that I'm trying to use is this:

sed -i '/^;date\.timezone[[:space:]]=.*$/date.timezone = "America/Mexico_City"' php.ini

But I get this:

sed: -e expression #1, char 35: extra characters after command

I don't really know why but if I leave just a character it works (not the expected way but it doesn't return an error). Example:

sed -i '/^;date\.timezone[[:space:]]=.*$/d' php.ini

This just removes the line ;date.timezone =

I'm really new to sed and I'd really appreciate any help.

Thank you


You are trying to substitute one pattern for another, so you need to use the sed s command, of the form


Also, you need to escape any / characters within the pattern or replacement strings (in your case "America\/Mexico_City"). So

sed 's/^;date\.timezone[[:space:]]=.*$/date.timezone = "America\/Mexico_City"/'

Alternatively, you can avoid escaping slashes by using an different pattern-replacement delimiter such as s#pattern#replacement# e.g.

sed 's#^;date\.timezone[[:space:]]=.*$#date.timezone = "America/Mexico_City"#'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.