Introduction
Knowing which run you are using on your system is an important piece of information. Your shell determines your login environment to a large extent as it controls which environment variables get exported, your shell prompt etc. On a Linux system it’s almost certain that you will using the bash shell unless the system administrator has deliberately changed it to something else. In this quick article we will demonstrate four ways you can determines if you are running the bash shell or not.
Method 1: Use the BASH environment variable
If you are currently using the bash shell then the BASH environment variable will contain the path of the bash shell installed on your system. If you are using a different shell then this variable will be empty. Let’s try this on the command line.
[root@nclient ~]# echo $BASH /bin/bash
The above output shows the path where bash is installed on my system. Now let’s change the shell and then try again.
[root@linuxnix ~]# zsh [root@linuxnix]~# echo $BASH [root@linuxnix]~#
When I changed my shell to zsh and then checked the value of the variable $BASH it came out to be empty indicative of the fact that I’m no longer running the bash shell.
Method 2: use the SHELL environment variable
The SHELL environment variable contains the path of the shell that is set when you log in to the system i.e the shell specified in your logged in users’ /etc/passwd file entry.
[root@linuxnix ~]# echo $SHELL /bin/bash [root@linuxnix ~]#
As you can see from the above output the SHELL variable contains the path of the bash shell. If we check the /etc/passwd entry for the root user we will find that the default shell for the root user is /bin/bash.
[root@linuxnix]~# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash
But if we change the shell to something else the output of the SHELL variable will still contain the path of the bash shell.
[root@linuxnix ~]# zsh [root@linuxnix]~# [root@linuxnix]~# echo $SHELL /bin/bash
Method 3: use PID of currently executing process
In this method we will use the $$ internal variable. The $$ variable contains the process id of the currently executing process. For our example, the $$ variable will contain the process id of the shell itself. We will use the value contained in the $$ variable as the input to the xargs command. The xargs command will then use that input process id, execute the ps command with that input and provide the process name or the command which in this case will be the name of the shell. Here is a demonstration.
[root@nclient ~]# echo $ | xargs ps PID TTY STAT TIME COMMAND 3007 pts/0 Ss 0:00 -bash
The value of $$ was 3007 which was the PID of our current shell process. We piped it to the xargs command and executed the ps command with the value contained in the $$ variable as input and obtained the above output on the terminal.
Method 4: Using $0 variable
This Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands, $0 is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero. Given below is an example.
[root@nclient ~]# echo $0 -bash
Conclusion
This includes our demonstration on four methods to identify your current shell for your logged in user. We hope that you’ve found this article to be useful and we look forward towards your suggestions and feedback.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021