How to return pid of a last command in Linux / Unix

I need to find and return the PID of the last command executed in Linux. How do I return the PID of command in Linux?

One can easily find the PID of the last executed command in shell script or bash. This page explains how to get the PID of a last executed app/program.

How to return pid of a last command in Linux

The syntax is as follows:

  1. Open the terminal application
  2. Run your command or app in the background. For example: firefox &
  3. To get the PID of the last executed command type: echo "$!"
  4. Store the pid of the last command in a variable named foo: foo=$!
  5. Print it, run: echo "$foo"

The PID of the last executed command is in the $! variable

The bash treats several variable specially. These vairable may only be referenced and assignment to them is not allowed. For example, the $! expands to the process ID (PID) of the command/program most recently placed into the background, whether executed as an asynchronous command or using the bg command/builtin.

How to get pid of just started process

Let us start sleep process, run:
sleep 20 &
echo $!

firefox &
echo $!

The PID of the last executed command displayed using special shell variable

Shell script example

Let us create a shell script wrapper named

echo "Starting my Linux/Unix awesome-app..."
/path/to/your/awesome-app &
echo "$_pid" > /var/run/
echo "Pid $_pid stored in /var/run/"

Using jobs internal command

The jobs command show status of jobs such as:
sleep 20 &
To lists process IDs only pass the -p option to the jobs:
my-app &
jobs -p

Sample outputs:


The jobs command works with ksh and other shells. Typically, I put something as follows in my shell script wrappers:

((${_name}) & jobs -p >${_pid})

It is also possible to get get the command's PID even before it runs using the following syntax:
bash -c 'echo $$; exec /path/to/my-command'
However, I recommend to stick with $_ or jobs -p method to follow KISS (keep it simple stupid) principle.


You learned how to find and display the process ID of last executed application or programe in Linux and Unix-like system using variuous methods. For more information see bash man page by typing the following man command:
man bash
help jobs

Sample outputs:

jobs: jobs [-lnprs] [jobspec ...] or jobs -x command [args]
    Display status of jobs.
    Lists the active jobs.  JOBSPEC restricts output to that job.
    Without options, the status of all active jobs is displayed.
      -l	lists process IDs in addition to the normal information
      -n	lists only processes that have changed status since the last
      -p	lists process IDs only
      -r	restrict output to running jobs
      -s	restrict output to stopped jobs
    If -x is supplied, COMMAND is run after all job specifications that
    appear in ARGS have been replaced with the process ID of that job's
    process group leader.
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.
    If -x is used, returns the exit status of COMMAND.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Comments are closed. Still have questions? Post it on our forum