Out of the box, Linux provides a wide variety of shells. There is bash (Bourne Again shell) shell which ships by default in many Linux distributions. We also have sh (Bourne Shell), tcsh (TC shell), csh (C shell), Zsh (Z shell) and ksh (Korn Shell).
Curious to know which shell you are using on your Linux system? In this guide, we explore different ways that you can use to check which shell you are currently using in Linux.
1) Using echo command
The Linux echo command is a built-in command that is used to print the output of a string which is passed as an argument. Additionally, you can use the echo command to check the shell that you are running commands in. To accomplish this, execute:
$ echo $SHELL
The output shows that I am using the bash shell. Additionally, you can simply run the command:
$ echo $0
To get the PID of the shell that you are currently in, run:
$ echo $
2) Using ps command
Commonly used for listing running processes, the ps command in its basic format sheds light on the shell that you are using. Simply execute the command:
From the first line of the output, we can clearly see the PID of the shell and the last column prints out the type of shell, in this case - bash.
Alternatively, you can run the command:
$ ps -p $
You can also use
ps -p $$ -o args= which output just the shell name.
3) By viewing /etc/passwd file
The grep command can be used to probe the
/etc/passwd file that contains attributes of the users such as username, user ID and group ID.
To display the shell used, invoke the command:
$ grep "^$USER" /etc/passwd
At the very last segment, we get to see the bash used, in this case
/bin/bash This also gives you a glimpse of which shell is opened first when you first log in to your system.
4) Using lsof command
Ordinarily, the lsof command, short for list of open files, is used to provide a list of open files on your system. However, when used with the
-p $$ flag, it gives a pointer to the shell you are in when you look at the first column of the output.
For example, we can clearly see that we are on the bash shell.
$ lsof -p $
How to check the valid login shells
We have seen various ways that you can employ to check the shell that you are currently in. If you want to know the valid shells on your system, check the
/etc/shells file, This file will provide you with the full pathnames of valid login shells in your system. Using the cat command, view the file as shown:
$ cat /etc/shells
In this guide, we have shared simple but nifty ways that you can use to know which shell you are working on. This is important when writing scripts so that you can know how to start off writing the shebang header. We do hope that this tutorial was beneficial. Send us a shout and don't forget to share this guide on your social platforms.