Retrieving the PID of Your Current Shell Session

Shell Scripting @

Process management is a crucial aspect of working with a Unix-like operating system. Understanding how to identify and manipulate processes is essential for tasks like monitoring system performance, terminating unresponsive programs, or even automating system tasks. One fundamental aspect of process management is retrieving the Process ID (PID) of your current shell session. In this article, we will explore the concept of PID, discuss its significance, and provide you with a step-by-step guide along with practical examples to help you write a script to display the PID of the current shell session.

What is a PID?

A Process ID (PID) is a unique numerical identifier assigned to each running process in a Unix-like operating system. PIDs are essential for tracking, managing, and controlling processes efficiently. They are used to perform various operations on processes, such as starting, stopping, or querying their status.

Why Do You Need to Know Your Current Shell Session’s PID?

Knowing the PID of your current shell session can be beneficial in several scenarios:

  1. Scripting: Automate tasks that require interaction with your shell session.
  2. Debugging: Identify and troubleshoot issues with running processes.
  3. Resource Management: Monitor resource utilization of your shell session.

Getting Your Current Shell Session’s PID

To retrieve the PID of your current shell session, you can use the built-in $$ variable in Unix-like shells, such as Bash or Zsh. This variable contains the PID of the current shell process.

Here’s a simple example of how to display your shell session’s PID:

echo "My current shell session PID is $$"


My current shell session PID is 52545

Writing a Script to Display the Current Shell Session’s PID

If you want to create a script that displays the PID of your current shell session, follow these steps:

  1. Create a new file: Using your favorite text editor, create a new file. You can name it something like
  2. Open the file: Open the file in your text editor and add the following lines:
# This script displays the PID of the current shell session.
echo "My current shell session PID is $$"
  1. Save the file: Save the file and exit your text editor.
  2. Make the script executable: In your terminal, navigate to the directory where you saved the script and make it executable using the following command:
chmod +x
  1. Run the script: Execute the script by running:

The script will display your current shell session’s PID.

Author: user