1

I had a typo and wrote $$ in a bash script (as argument to echo). It printed a number (1979). I tried many times, and all of them printed the same number, so I thought it might have to do with the Unix epoch. I didn't find any information about it in the bash manual, or in the internet at all. Then I tried on a basic sh; it printed a different number, 5755, and after closing and opening a new sh, it printed a slightly different number.

What is that variable supposed to hold? What is the meaning of that number?

  • @muru yes it does. My search foo failed :/. Maybe the special characters don't work very well in the search engine... – alx - recommends codidact Apr 18 '21 at 12:26
  • Actually, https://unix.stackexchange.com/q/218270/341413 is better, even if it's less specific. The answers to the question you pointed to didn't specify that subshells get the same result. – alx - recommends codidact Apr 18 '21 at 12:29
  • That's pretty much how I found the dupe. Search for "bash special variable", find that question, look at the linked ones for a more specific one – muru Apr 18 '21 at 12:33
  • 1
    Searching for those can be a pain, and it's not helped by the fact that $$ and others like it aren't technically variables, just parameters, the distinction being a bit of hair-splitting. "Variables" are parameters with alphanumerical names (not starting with digits), and only variables can be set with e.g. var=value. Also, $foo is parameter expansion: it can be used with non-variable parameters too. Though in practice people of course use "variable expansion" to mean the same thing, and e.g. Bash's manual uses the phrase "parameter and variable expansion". – ilkkachu Apr 18 '21 at 12:58

2 Answers2

3

It's one of a number of special shell variables. From the POSIX standard relating to this:

$

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.

Other special shell variables include ! (the PID of the most recently started background task), ? (the exit status of the most recent task terminating), # (the number of positional parameters), * (the positional parameters, concatenated into a single string), @ (the positional parameters, as a list), - (the current shell options), and 0 (zero; the name of the shell or script).

Also, from the bash manual's "Special Parameters" section:

$

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

Other shells will have a similar wording in their manuals.

In short, the $ variable, whose value you get by typing $$, holds the process ID (PID) of the current shell process.

You can double-check this like so:

% echo "$$"
85776
% ps -p "$$"
  PID TT  STAT        TIME COMMAND
85776 p1  SN       0:00.19 -zsh (zsh)

This shows that $$ expands to 85776 for my particular shell process, and that this PID does indeed correspond to a zsh login shell, which is what I'm using.

Kusalananda
  • 333,661
0

That variable shows you the current shell's PID, or process ID. Every time you echo (or access) that variable in a given shell, it will be the same. Anytime you start a new shell (or a subshell) and access it, it will be different from the original shell.

John
  • 17,011
  • The value of $$ in a subshell is always the same as that of the surrounding shell. If you want the PID of a particular subshell, you will have to use $BASH_PID, at least if you're using the bash shell. – Kusalananda Apr 18 '21 at 12:17