0

As the title suggests, I am wondering what the whole point of echo $$ is... I understand that the command echo $? returns the status of the last command that was ran and now I am stumped on what echo $$ returns... I believe it runs something related to PID's, am I somewhat correct or totally wrong?

NerdOfCode
  • 2,498

1 Answers1

3

From POSIX standard definition:

$

Expands to the decimal process ID of the invoked shell. In a subshell (see Shell Execution Environment ), '$' shall expand to the same value as that of the current shell.

This is necessary when you might send signals, such as KILL or SiGTERM to shells other than you're currently using, determining process info about the shell via data available in /proc/$$/ directory, and many other things. I vaguely recall using it once in shell script to capture script's PID, and then distinguish it from function calls defined within the script. It can also be used to distinguish multiple instances of a script of the same name.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497