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

So, I have the following line that I run in PHP with exec($addPHPtags);

$addPHPtags = "/bin/sed -i '/<BODY BGCOLOR=\"#FFFFFF\">/ a\ <?php \n ?> '" . $instance['file'] . " 2>&1";

I'd expect that command to find the key and append it with a php tag. However, when I run it in PHP, I get the following error if I trap command output:

[0] => /bin/sed: -e expression #1, char 39: unknown command: `?'

However, if I run the same command in SSH, it works completely fine:

/bin/sed -i '/<BODY BGCOLOR=\"#FFFFFF\">/ a\ <?php \n ?>' file.php

I'm out of ideas, I've tried various alternatives but to no avail. Any help ? Thanks.

share|improve this question
up vote 1 down vote accepted
/bin/sed/ -i '/<BODY BGCOLOR="#FFFFFF">/ a\ <?php \n ?> 'INPUTFILE` 2>&1

Notice that a space is lacking between the regex and your input file.

$addPHPtags = "/bin/sed -i '/<BODY BGCOLOR=\"#FFFFFF\">/ a\ <?php \n ?> '" . $instance['file'] . " 2>&1";
#                                                                       ^^

Put a space there in your PHP, and I think it might be ok... and double-escape the newline. Why aren't you doing the transform directly in PHP?

share|improve this answer
Great, I've had to double escape the linebreak and the issue was the lacking space. Silly - really. Thanks for the great help ! – fooraide Dec 28 '10 at 23:21
Yes, I'd like to know why it's not being done in PHP with something clever around preg_replace? – Tom O'Connor Dec 28 '10 at 23:39

You probably need to double escape the newline. Try:

$addPHPtags = "/bin/sed -i '/<BODY BGCOLOR=\"#FFFFFF\">/ a\ <?php \\n ?> '" . $instance['file'] . " 2>&1";
share|improve this answer
Double escaping the linebreak fixes (or not?) the error but instead shows a different one: [0] => /bin/sed: no input files – fooraide Dec 28 '10 at 19:40

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.