Variable Expansion between Backticks
01 March 2009
I expected that the following script would print a error number. But it actually prints 0.
#!/usr/bin/perl `echo hello world`; $retval=`err-cmd >/dev/null 2>&1;echo $?`; print ("result=$retval");
What is wrong? learning perl sec14.4 says:
The value beween backquotes is like the single-argument form of system and is interpreted as a doublequoted string, meaning that backslash-escapes and variables are expanded appropriately.
When perl interprets the aforementioned script, it will replace the $?
with
return value of the command before it. Since that value is 0 (the return value
of echo hello world
), the script is equivalent to this:
`echo hello world`; $retval=`err-cmd >/dev/null 2>&1;echo 0`; print ("result=$retval");
The fix is straight forward: escape the dollar sign:
$retval=`err-cmd >/dev/null 2>&1;echo \$?`;
Please note that shell scripts has a similar issue:
$ cat test.sh #!/usr/local/bin/bash eval "touch /file/not/exist 2>/dev/null; echo $?" echo "$(touch /file/not/exist 2>/dev/null) $?" $ ./test.sh 0 1
blog comments powered by Disqus