Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I have a shell file which needs to perform a replace of some text in a php file.

The line that needs replacing is:

$database = $db."_db";

The actual sed command needs to contain a variable so not sure what i should be escaping and where?


sed -i s/$db."db"/$DATABASE/ mysql_connect.php


share|improve this question
up vote 3 down vote accepted

Enclose the string in single quotes to prevent variable expansion.

sed -i s/'$db."_db"'/$DATABASE/ mysql_connect.php

to replace all occurrences of $db."_db" with the value of $DATABASE.

share|improve this answer
perfect thanks :) – seengee Jul 24 '12 at 11:19

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.