Take the 2-minute tour ×

I'm trying to replace instances of {$pear_root} in a config file with the $PEAR_ROOT variable in a shell script.

Using the method from here under "A Note About Bash Escape Character" I have:

sed -i "s/{\$pear_root}/${ESC_PEAR_ROOT}/"../.serverconfig/test.txt

I'm getting back 34: Bad substitution which I think may mean that this is unsupported.

The problem seems to be the use of s/find/replace/, as I get the same error whether doing this with sed or with perl.

What is the simplest way to do this:

APP_ROOT=$(php ./scripts/get_realpath.php '../')
sed -e "s/{\$pear_root}/${ESC_PEAR_ROOT}/"../.serverconfig/test.txt

And end up with all instances of {$pear_root} in test.txt replaced with the path from PEAR_ROOT?

I'm not attached to this method - anything reliable will do.

share|improve this question
what are you trying to set ESC_PEAR_ROOT to instead of PEAR_ROOT? Can you give an example? –  evil otto Feb 8 '12 at 22:35
You haven't escaped the $ in the second part of the expression - does that change things? –  John3136 Feb 8 '12 at 22:37
@evil otto: I think the ESC_PEAR_ROOT will end up with \/ where every slash used to be. I need that for this one statement, but I also need the original PEAR_ROOT for later use. So if I escape PEAR_ROOT, I won't be able to use it as a path later. –  Eli Feb 8 '12 at 22:43
@John3136: I think I want that to be expanded. If I escape the $, then the file will end up with $PEAR_ROOT in it instead of the contained path. I think. Shell scripting is not my forte =o) –  Eli Feb 8 '12 at 22:45
@evil otto: Oh, you mean what is the path? In this server it's /var/www/rmc/pear. The escaped version will be \/var\/www\/rmc\/pear, which should (hopefully) cause the placeholder in the file to be replaced with the unescaped path. Sorry, I read your last comment as why, not what. –  Eli Feb 8 '12 at 22:47

2 Answers 2

This is a bit silly, but I notice you're missing a space between the end of your expression and the name of the file to which you wish to apply it.

/tmp $ sed -e 's/{$pear_root}/'"$PATH"'/' test
sed: bad option in substitution expression
share|improve this answer
Yes, good catch. I got that fixed, but the extra "$" is what was killing it. –  Eli Feb 8 '12 at 23:00
up vote 2 down vote accepted

Ok, solved it.

The problem was:



Should have been this:


That was what was causing my "bad substitution" error.

Also, realized I didn't need to escape, since I could just use a different delimiter to s, so I don't even need that line.

Final result ended up as:

sed -i "s#{\$pear_root}#${PEAR_ROOT}#g" ../.serverconfig/test.txt
share|improve this answer

Your Answer


