how to determine the current command line shell in linux

Linux has many different command line shells that can be used. And different Linux distros use different shells as default. Sometimes you might also be using different shells in different terminals.

You can use sh, bash, csh, ksh, zsh among the very many that are available. Most times it is not obvious from the command line as to what shell is currently being used, especially if you are logging onto a new machine or if you use different shells in different terminals. Also, different users can have different shells as the default shell.

There are several ways to find which shell you are currently using.

Using Environment Variables

One way is to see what the environment variable SHELL says you are using. You can use either of the two options below to see the value of the shell variable.

bash$ echo $SHELL

This will print out the value of the variable of the SHELL, and it should say something like /bin/bash or /bin/csh or sh. Printing out all the variables which contain the word is shell is another way to view it.

bash$ env | grep -i shell

The output of the above command could be something like what is shown below. You can see the SHELL value as well as other information. The output will vary depending on the environment.

SHELL=/bin/bash
KONSOLE_PROFILE_NAME=Shell
SHELL_SESSION_ID=13984ad514c64000ac0305a6f3d9efa6

Another method is to use the shortcut ‘0’ as shown below. This is better because the environment variable SHELL might show you the “wrong” shell if you happen to have executed one shell on top of another.

So, as an example if you executed sh on top of a bash shell, then the SHELL variable will still have bash as the underlying shell, even though your command line is interpreted by sh.

bash$ echo $0

Using the process information

In order to see all the shells that might be running, including the current command line shell use the ps command to see all the running processes in the terminal. This will give you a list of currently running processes including the shells, but if you find that there are multiple shells, then one might be running as a child of the other.

bash$ ps

You can also one of the following to just print out just the command line shell, without the other processes that might also be running.

bash$ ps | grep $$

The below is just the same as before but without the use of grep to filter the output.

bash$ ps -p $$

If you want to execute another shell in the current terminal then use exec to execute the shell, like below.

bash$ exec /bin/csh

The above command will execute the csh shell, and all your subsequent commands on this terminal will be executed in the csh shell. This is also something that you can put in your .bashrc file, so that all terminals that are opened will execute that and use it. The is usually useful, if by default you have one particular shell defined but want to use another as default, but do not have the permissions to change the default on the system.