...

How do I find out what shell I am using on Linux/Unix? nixCraft Updated Tutorials/Posts

how-do-i-find-out-what-shell-i-am-using-on-linux-unix-nixcraft-updated-tutorials-posts

Both Linux and Unix provides various shell out of the box. One can find bash (Bourne Again shell), ksh (Korn shell), csh (C shell)/tcsh (TC shell), sh (Bourne shell) and more installed by default. However, how do you check which shell am I using? What is the best way to find out what shell I am using on Linux? The echo $SHELL is not so reliable. This page explains how to find out which shell I am using at a Linux, MacOS, FreeBSD, or Unix-like systems.

How to find out what shell I am using?

The following echo command or printf command should work:
echo "$SHELL"
OR
printf "My current shell - %s\n" "$SHELL"
Please note that $SHELL is the shell for the current user but not necessarily shell that is running at the movement. Try the following examples

bash echo "My current shell is $SHELL ($0)"
ksh ## start a new shell ## echo "My current shell is $SHELL ($0)" echo $SHELL

Find out what shell I am using on Linux or Unix
Hence, I recommend using the following syntax to check which shell you are using.

How do I check which shell am I using?

Here is another old good Unix trick. Use the ps command with -p {pid} option. The following command selects the processes whose process ID numbers appear in pid. Use the following command to find out which shell you are in:
ps -p $$
Sample outputs:

 PID TTY TIME CMD 5217 ? 00:00:00 bash

So what is a $ argument passed to the -p option? Remember $ returns the PID (process identification number) of the current process, and the current process is your shell. So running a ps on that number displays a process status listing of your shell. In that listing, you will find the name of your shell (look for CMD column).
ps -p $$
Sample outputs:

 PID TTY TIME CMD
6453 pts/0 00:00:00 csh

From my Linux box:
ps -p $$
Sample outputs:

 PID TTY TIME CMD
5866 pts/0 00:00:00 bash

You can store your shell name in a variable as follows :
MYSHELL=`ps -hp $$|awk '{echo $5}'`
Please note those are backquotes, not apostrophes. Or better try out following if you have a bash shell:
MYSHELL=$(ps -hp $$|awk '{echo $5}')
Another option is as follows:
echo $0
OR
printf "%s\n" $0
Sample outputs from above commands:

Fig.01: Linux check which shell am I using
Fig.01: Linux check which shell am I using

How do I check how many shells are installed on my Linux box?

The /etc/shells is a text file which contains the full pathnames of valid login shells. Type the following [nixmd name=”cat”] to see list how many shells are installed on your Linux or Unix box:
cat /etc/shells

Linux or Unix check how many shells are installed
Use /etc/shells file to check how many shells are installed on your system

Okay, so when I open the Terminal app, which shell is opened by default?

Your default shell defined in /etc/passwd file. So try the following grep command:

grep "^$USER" /etc/passwd awk -F: '/vivek/ { print $7}' /etc/passwd

When I open a terminal, which shell is opened by default on Linux
Look like /bin/bash shell is my default shell. Want to change your default shell? Read “change shell in Linux or Unix” FAQ page for more info. Let us summaries all command once again.

How to check which shell am I using:

Use the following Linux or Unix commands:

  1. ps -p $$ – Display your current shell name reliably.
  2. echo "$SHELL" – Display the shell for the current user but not necessarily shell that is running at the movement.
  3. echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.
  4. cat /etc/shells – List pathnames of valid login shells currently installed
  5. grep "^$USER" /etc/passwd – Print the default shell name. The default shell runs when you open a terminal window.
  6. chsh -s /bin/ksh – Change the shell used from /bin/bash (default) to /bin/ksh for your account

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading