I'm using bash on a RH Linux system.

Normally, you can get your own PID with the variable $$. However, if a script runs one of its own functions as a background process - that doesn't work; all functions run in the background will get the PID of the parent script when $$ is used.

For example, here is a test script:

    echo "I am $$"
    function proce {
      sleep 3
      echo "$1 :: $$"

    for x in aa bb cc; do
      eval "proce $x &"
      echo "Started: $!"

When it is executed:

    /tmp$ ./test
    I am 5253
    Started: 5254
    Started: 5256
    Started: 5258
    /tmp$ aa :: 5253
    bb :: 5253
    cc :: 5253

So - the parent script (/tmp/test) executes as PID 5253 and fires-off three background processes with PIDs 5254, 5256 and 5258. But each of those background processes gets the value 5253 with $$.

How can these processes discover its actual PID?

  • Just a pedantic note: your example does not background the subshells, everything is done in the foreground.
    – hildred
    Commented Apr 7, 2015 at 4:10

2 Answers 2


$BASHPID may be what you are looking for.


Expands to the process ID of the current Bash process. This differs from $$ under certain circumstances, such as subshells that do not require Bash to be re-initialized.

As opposed to $$

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



With bash:

echo "$BASHPID"

would give you the pid of the process that evaluates that echo command

Note that (for instance after a enable -n echo) it is not necessarily the same as the pid of the process that is running that echo command.

bash (or any shell) does its own soup with processes. It's not always useful to try and guess what process does what.

$ readlink -f /proc/self "/proc/$BASHPID"
$ (readlink -f /proc/self "/proc/$BASHPID")

In that second case, readlink is executed in the same process that interpreted the command line because that's the last command run in that subshell (so bash optimises out a fork()).

The zsh equivalent is in the zsh/system module:

$ zmodload zsh/system
$ echo $$ $sysparams[pid]
5155 5155
$ (echo $$ $sysparams[pid])
5155 30979

(zsh also exposes the parent pid of the subshell in $sysparams[ppid]).

Portably, you can do:

pid=$(sh -c 'echo "$PPID"')

All shells should run that sh as a direct child of the subshell interpreting that command line, so that sh's $PPID should be that subshell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .