A process ID (PID) is a unique number assigned by the Linux kernel to each running process. Shells such as Bash also run as processes, which means every active shell session has its own PID. Knowing the PID of the current shell can be useful when working with process management, shell scripting, debugging, or monitoring. This tutorial provides 3 methods how to get shell PID on Linux.
Method 1 - $$ special parameter
The $$ is a special shell parameter that expands to the process ID of the current shell. The echo command can be used to display its value:
echo $$
Output example:
2232192
Method 2 - ps command
The ps command displays information about running processes. The -p option limits the output to a process with the specified PID. Since $$ contains the PID of the current shell, it can be passed directly to ps.
ps -p $$ -o pid=
The -o pid= option displays only the PID and removes the column heading.
Method 3 - BASHPID variable
The BASHPID variable is a Bash-specific variable that contains the process ID of the current Bash process. This method is therefore applicable when the current shell is Bash. The value can be displayed with the echo command:
echo $BASHPID
Leave a Comment
Cancel reply