Summary: The shell performs parameter substitution on strings in double quotes but not on strings in single quotes. $$
is the shell PID but the number you see depends on which shell evaluates it.
Details: Let us consider each case, one at a time.
[root@localhost ~]# echo $$
16991
16991 is the PID of the current shell: let's call it the main shell.
[root@localhost ~]# bash -c "echo $$"
16991
When main shell sees "echo $$"
, it will substitute in 16991 for $$
. Thus, the string passed to bash -c
will be echo 16991
. That is why 16991 is printed.
[root@localhost ~]# bash -c 'echo $$'
21062
Because 'echo $$'
has single quotes, the main shell does not do parameter substitution. The string echo $$
is passed to bash -c
. 21062 is printed because it is the PID of the the bash -c
process.
[root@localhost ~]# bash -c 'echo $$'
21063
The next time that you run the bash -c
process, it has a new PID, this time 21063.
$$
here - so these above are not duplicates. – Volker Siegel Mar 26 '15 at 19:57bash -c …
always creates a sub-shell (i.e., a new process) regardless of the type of quotes used. The quoting style affects only what command that sub-shell is asked to execute. – Scott - Слава Україні Mar 26 '15 at 20:17