Echo Command – Why Does `echo $$` Return a Number?

bashcommand lineecho

Why does running echo $$ in bash return a number like 7190, while running echo $ only returns a $?

Best Answer

Convention.

$$: Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell (see the link to the manual below).

rinzwind@schijfwereld:~$ echo $$
3244
rinzwind@schijfwereld:~$ ps -ef |grep 3244
rinzwind  3244  3237  0 19:06 pts/0    00:00:00 /bin/bash

Very useful when coding software. And it can be used as a crude (mktemp would be the better method) way of creating temp files

1 $ has no special meaning so it gives you what echo always does: return it.

There is a manual page dedicated to this (3.4.2 Special Parameters).

Related Question