Results 1 to 4 of 4
I have a line in a shell script that looks like this:
Code:
running=`ps -p $pid -o args= | sed -n "s?^$php_fpm_BIN\(\ .*\)*\$?&?p"`
But this gives an error about an ...
- 10-18-2010 #1Just Joined!
- Join Date
- Oct 2010
- Posts
- 5
Confused about escaping in backticks
I have a line in a shell script that looks like this:
But this gives an error about an unterminated `s' command. If I double escape the closing $ likeCode:running=`ps -p $pid -o args= | sed -n "s?^$php_fpm_BIN\(\ .*\)*\$?&?p"`
then it works.Code:running=`ps -p $pid -o args= | sed -n "s?^$php_fpm_BIN\(\ .*\)*\\$?&?p"`
Why does the $ need double escaping?
- 12-11-2010 #2
- 12-11-2010 #3Just Joined!
- Join Date
- Oct 2010
- Posts
- 5
- 12-11-2010 #4
You have to double-escape this because you are passing the dollar sign to an environment, which interprets it. If you write \$, it is interpreted as a raw $ and then passed to the expression in backticks. Therefore, the backtick environment tries to interpret it again.
To avoid this behavior either use $(…) (which is the newer and recommended way) or use single-quotes for your sed command.
You can easily check the different behaviors with these commands:Code:export FOO=bar echo `echo $FOO` echo `echo \$FOO` echo `echo "$FOO"` echo `echo "\$FOO"` echo `echo '$FOO'` echo `echo '\$FOO'` echo $(echo $FOO) echo $(echo \$FOO)
Refining Linux Advent calendar: “24 Outstanding ZSH Gems”


Reply With Quote
