683

I read that terminal is nothing but shell, and Unix provides different flavors of shells:

  • Bourne shell (sh)
  • C shell (csh)
  • TC shell (tcsh)
  • Korn shell (ksh)
  • Bourne Again shell (bash)

Questions:

  • When I open a terminal window, which shell is opened by default?
  • How do I check how many shells are installed?
  • How do I change the shell used from my account?
deltacoder
  • 7,005

9 Answers9

802

You can type the following command in your terminal to see which shell you are using:

echo $0

The result will look something similar to the below if you are using the bash (Bourne Again Shell) terminal:

-bash
kingmilo
  • 10,784
229

To find the shell you have on the default environment you can check the value of the SHELL environment variable:

echo $SHELL

To find the current shell instance, look for the process (shell) having the PID of the current shell instance.

To find the PID of the current instance of shell:

echo "$$"

Now to find the process having the PID:

ps -p <PID>

Putting it together:

ps -p "$$"
heemayl
  • 93,925
74

$SHELL gives you the default shell. $0 gives you the current shell.

For example: I have bash as my default shell, which I use for my Terminal App. But for my iTerm2 app, I use the command as the window opens: /bin/ksh.

So my $0 gives me /bin/ksh on iTerm2. $SHELL gives me /bin/bash on iTerm2. $0,$SHELL gives me /bin/bash on Terminal

muru
  • 207,228
26

The other answers tend to be using shell specific features, but we are trying to discover which shell we are using, so they assume the answer to the problem. For example none of the answers will work on fish.

sh -c 'ps -p $$ -o ppid=' | xargs ps -o comm= -p

Instead use the $$ syntax in an invocation of sh, but then we are looking for the PPID not the PID. Use the PPID to find the cmd.

sh -c 'ps -p $$ -o ppid=' | xargs -I'{}' readlink -f '/proc/{}/exe'

Thanks for improvement @muru

Evan Benn
  • 359
15

The original post asked three questions. The answers given do cover the first question, "When I open a terminal window, which shell is opened by default?" They also answer a question which was NOT asked, namely "How can I find out which shell is currently running in the terminal?" However, as far as I can see nobody has answered either the second or third questions originally asked, namely "How do I check how many shells are installed?" and "How do I change the shell used from my account?"

  • To answer "How do I check how many shells are installed?" the following command will list all the available shells:

    cat /etc/shells
    

    For example, on a default installation of Ubuntu 18.10 this gives:

    # /etc/shells: valid login shells
    /bin/sh
    /bin/dash 
    /bin/bash
    /bin/rbash
    

    However, by default sh is a symbolic link to dash, while rbash links to bash with the option -r ("restricted bash") so there are actually only two shells, not four as the above list suggests. The following command will show you whether any of the listed shells are in fact symbolic links, and if so where they link to: ls -l /bin

  • Now for the question "How do I change the shell used from my account?" Assuming this means "How do I permanently change the default shell that a terminal will use", there is an answer here.

Pablo Bianchi
  • 17,371
Michael D
  • 319
13

From man login:

The environment variable values for $HOME, $USER, $SHELL, $PATH, $LOGNAME, and $MAIL are set according to the appropriate fields in the password entry.

So to print the default shell for your user, you can run:

printf '%s\n' "${SHELL}"

Off the top of my head I can't think of a reason why SHELL could be unset / overriden, but to accomplish the exact same without relying on it:

getent passwd $(id -un) | cut -d: -f7

Note that my shell is Zsh, and I'm running these commands in Bash:

> printf '%s\n' "${SHELL}"
/usr/bin/zsh
> getent passwd $(id -un) | cut -d: -f7
/usr/bin/zsh
kos
  • 41,268
6

You may not want to know the current shell's name (e.g. -bash, bash, zsh, etc., from echo $0), nor default shell's executable path (from echo $SHELL), but rather the current shell's executable path (especially useful e.g. if you have more than one version of Bash installed).

To do this you can use lsof -p "$$" or with some extra coding to extract just the required info:

lsof -p "$$" | grep -m 1 txt | xargs -n 1 | tail -n 1

Example output for Bash installed via Homebrew:

/usr/local/Cellar/bash/5.1.8/bin/bash

or for Zsh:

/bin/zsh

The above is different from echo $SHELL, both because the above is for the shell which is currently running rather than the user's default shell, and also because the above gives the executable after any symlinks have been expanded. E.g. for the same Bash install as above, echo $SHELL gives /usr/local/bin/bash.

EDIT 1: If you need to allow for possible space characters in the shell's path, use lsof -p "$$" | grep -m 1 txt | xargs -n 1 | tail -n +9 | xargs instead.

EDIT 2: Yet another way to see the current shell's executable, this time not using lsof, is ls -l "/proc/$$/exe".

Converting this to a command which doesn't require lsof, allows for possible spaces in the shell executable path and allows for possible aliases of ls, we get:

"$(which ls)" -l "/proc/$$/exe" | xargs -n 1 | tail -n +11 | xargs

Note that this last version with /proc/$$ does not work on macOS, whereas the versions with lsof do, as well as on any Linux with lsof installed.

5

To address your third question, "How do I change the shell used from my account?", the answer is to use chsh.

There are two modes:

  • interactive, and;
  • non-interactive.

From Changing Shells - Changing your login shell which is permanent, and paraphrasing it slightly:

You will use a program called chsh. There is a interactive method and non-interactive method. Type the following into your terminal:

INTERACTIVE METHOD

$ chsh

This results in a brief dialog in which the user is prompted first for their password and then for the full path of the desired new shell.

Caution should be exercised when changing one's default shell because it is possible to make an error that only the root user (i.e., system administrator) can repair (although it should be easy for a skilled user to repair it on a home system). In particular, it is important to first test the shell temporarily in the current session and then to make certain that a valid shell name is being entered when making the permanent change.

NON-INTERACTIVE METHOD

I will use csh as again an example.

$ chsh -s /bin/csh

The -s sets it for you without having to go into the editor to do it.

Once this is executed successfully, then echo $SHELL will still say that you are in the same shell as before. However, you need to log out and back in for the change to take effect. Now do echo $SHELL. You should see it shows the new shell.

Greenonline
  • 2,182
0

In one of the servers I connect to, the login shell is /bin/sh which is a symlink to /bin/bash

Most answers here will give sh, which would make the OP consider it's Bourne shell and not GNU bash, except this one that gives /bin/bash

Another option that works for this case is:

$ echo $SHELL
/bin/sh

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May 31 16:15 /bin/sh -> bash

$ /bin/sh --help
GNU bash, version 4.2.10(1)
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
golimar
  • 127