Learn Difference Between $$ and $BASHPID in Bash

Recently I was working on a shell script and I saw a significant difference in how bash special variable $ and BASHPID behaves. Every process running in Linux will be assigned with a process ID and that is how the operating system handles the process.

Similarly, your bash terminal session will also be assigned with a process ID. There is a special variable called "$" and "$BASHPID" which stores the process ID of the current shell.

Go ahead and run the below command to see what is the process ID of your current shell. Both "$" and "$BASHPID" is going to return the same value.

$ echo $$               # Printing special variable $
$ echo $BASHPID         # Printing the varibale $BASHPID
Find Current Shell Process ID
Find Current Shell Process ID

In bash when we call any external program from the shell, it will create a child process/subshell and the program will be submitted in the child process only. See below example where I put a simple process monitor command in a script called “sample.sh” to demonstrate how the parent shell creates a subshell to run the program.

#!/usr/bin/env bash

ps -ef --forest | grep -i bash

Now on running this script we can get the process ID of bash. From the below image, you can understand when I called the script bash creates a child process and run the script.

$ ./sample.sh
Linux Child Process
Linux Child Process

Now let’s use both "$" and "$BASHPID" inside the script and see what it returns.

#!/usr/bin/env bash
echo "============================"
ps -ef --forest | grep -i bash
echo "============================"
echo "PID USING $ FOR SCRIPT $0 ==> $$"
echo "PID USING BASHPID FOR SCRIPT $0 ==> $BASHPID"
echo

Now run the script again.

$ ./sample.sh
PID of Child Process
PID of Child Process

All right, it returns the same process ID. Here comes the actual difference. Let’s create another child process inside the script by running a command inside parentheses().

# STORING THE PID INTO A VARIABLE…

VAR_HASH=$(echo $$)
VAR_BASHPID=$(echo $BASHPID)

echo "VALUE OF VAR_HASH ==> $VAR_HASH"
echo "VALUE OF VAR_BASHPID ==> $VAR_BASHPID"
Difference in Pid
Difference in Pid

In bash, Parentheses will invoke a child process and run whatever comes inside the parentheses. In that case, both $ and $BASHPID should store a new child process ID. But from the above image, you can see there is a difference where $ stores 382 which is the parent ID (Process ID of the script sample.sh), and $BASHPID stores the created child process ID created by parentheses.

Now let’s try to understand this behavior. We will see what the man page says.

$ man bash
Man Page for $
Man Page for $
Man Page for Bashpid
Man Page for Bashpid

When you use $, even in a subshell, it stores the process ID of the parent process it got created from. But BASHPID will store the current process ID, i.e when called inside parentheses it will store the child process ID.

We cannot assign or modify the variable $, but BASHPID can be reassigned but it has no effect.

$ $=10
$ BASHPID=10
$ echo $BASHPID
Bash Variable Assignment
Bash Variable Assignment

It is possible to unset BASHPID. When you unset it loses its special state and also you can start using this as a normal variable.

$ unset BASHPID
$ echo $BASHPID
$ BASHPID="Tecmint"
$ echo $BASHPID
Unset Bashpid
Unset Bashpid

Even if you try to assign the process ID of the shell it will be treated as a user-defined variable since it already lost its special state.

$ BASHPID=$(echo $$)
$ echo $$;echo $BASHPID
Reassigning BASHPID
Reassigning BASHPID

In this case, you have to use a new terminal session for BASHPID to get its special state.

That’s it for this article. We have seen the difference between $ and BASHPID and how they behave in this article. Go through this article and share your valuable feedback with us.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Karthick
A passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology, especially about Linux and open source.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.