0

Why do I get a number when doing that :

echo $$

which returns

489

If I open a new terminal it returns another number. It seems it's related to the pid of the terminal session, but why ?

mklement0
  • 382,024
  • 64
  • 607
  • 775
jrjc
  • 21,103
  • 9
  • 64
  • 78

3 Answers3

2

$$ means your current PID.

As seen in Bash Reference Manual - 3.4.2 Special Parameters:

$

Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.

You can test it by doing ps -ef | grep 489, and it will show the process in which you are logged in.

For example in my case:

$ echo $$
3470

$ ps -ef | grep 3470
1000      3470  3469  0 10:59 pts/3    00:00:00 -bash    <---- this process
1000      8151  3470  0 15:37 pts/3    00:00:00 ps -ef
1000      8152  3470  0 15:37 pts/3    00:00:00 grep --color=auto 3470
fedorqui
  • 275,237
  • 103
  • 548
  • 598
2

Because that's how it is defined. $$ is a special shell variable (like e.g. $!, $_, $@, $1, ...) referring to the PID of the invoked shell.

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
1

You will find an excellent explanation in this post.

$$ pid of the current shell (not subshell)

Community
  • 1
  • 1
Saucier
  • 4,200
  • 1
  • 25
  • 46