I was wondering why this command line does not print
$ sh -c "var=`pidof sh` ; echo \$var; ps -H"
This one prints
123 as expected
$ sh -c "var=123 ; echo \$var; ps -H"
I am on Ubuntu Linux.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
You miss the quotes. One way to do it:
sh -c "var=\"`pidof sh`\" ; echo \$var; ps -H"
sh -c "var=\"\`pidof sh\`\" ; echo \$var; ps -H"
Notice that they differ in the moment when the
pidof sh is executed! In the first version, the expression within backticks (
pidof sh) is executed by your current shell - that is before
sh -c is run. In the second, it is the
sh -c command that executes
pidof. More than that, the
pidof is executed within a subshell that evaluates the expression within backticks - so you get one additional pid listed in the
var variable. (Put it more simple: the backticks invoke another shell which is then listed by pidof.)
A better way for both would be to use
$( ... ) or
\$( ... ).