Get the PID of the last run command

Back to index
Date: 2021-06-24
Category: bash


If the previous command is still running, it's possible to get the PID of the last run commad using the $! variable.

➜ til git:(master)  sleep 15 &
[1] 7652
➜ til git:(master)  kill $!
[1]+  Terminated              sleep 15

This also works if the command has terminated.

➜ til git:(master)  sleep 2
➜ til git:(master)  echo $!
7652