Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Learn Difference Between $$ and $BASHPID in Bash - DesignLinux

Feb 15 2021
designlinux 0 Comments

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.

Related

Tags: Bash Tips

How to Install Microsoft Edge Browser on Ubuntu 20.04

Prev Post

Linux Foundation Certified IT Associate (LFCA)

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,494
  • 617,183

DesignLinux.com © All rights reserved

Go to mobile version