Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following works great:

phpmemory_limit=256M #or what ever you want it set to
sed -i 's/memory_limit = 16M/memory_limit = '${phpmemory_limit}'/' /etc/php5/apache2/php.ini

If the memory_limit is set to 16M, but I've found that in some distributions it doesn't default to 16M, but instead will default to 32M. So my question is how to I have SED account for that and replace whatever the number is to ${phpmemory_limit}?

share|improve this question
up vote 4 down vote accepted

you can use regexp - for instance:

phpmemory_limit=256M #or what ever you want it set to
sed -i 's/memory_limit = .*/memory_limit = '${phpmemory_limit}'/' /etc/php5/apache2/php.ini
share|improve this answer
Thanks pQd, that did the trick. – BassKozz Dec 21 '09 at 16:55

Assuming that:

  • Values will always be specified in Mb.
  • You wish to preserve trailing comments.
  • You don't wish to modify any commented occurances.
  • phpmemory_limit only contains an integer.

    sed -ri 's/^(memory_limit = )[0-9]+(M.*)$/\1'${phpmemory_limit}'\2/' /etc/php5/apache2/php.ini

This uses..

  • Backreferences to shorten the regex and preserve the comment.
  • seds extended regex support.
  • It's important that the shortened options are specified in that order, otherwise it will create a backup with the file extension r.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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