Find the PID of a background child process in Bash


In order to control or monitor background child processes from a shell script you will need to know the PID of the child. Bash stores the PID of the last process executed in the “$!” shell variable.

If you start a background process in an interactive shell it will output the PID of the child.
$ sleep 1 &[1]20450

Since thee PID of the last command set to run in the background by the current shell or script is stored in ‘$!‘ variable, you can simply echo this variable to print the PID of the previous sleep command.
$ echo $!20450