How do I find out what shell I'm using?

Posted on in Categories CentOS, Debian Linux, FreeBSD, Gentoo Linux, Howto, Linux, Shell scripting, Sys admin, Ubuntu Linux, UNIX last updated November 15, 2005

Asked by Chetan Joshi:

Question: What is the best way to find out what shell I am using on Linux? The echo $SHELL is not so reliable. Please let me know any tiny command or trick.

Answer: Chetan, echo $SHELL should work. However, here is old good UNIX trick. Use the command ps with -p {pid} option, which selects the processes whose process ID numbers appear in pid. Use the following command to find out what 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 $$
Output:

  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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

33 comment

      1. No, it tells you the *default* shell for every user, not the one you are actually using in your current session.

        Use

        ps h p $$
         6710 pts/0    S      0:00 /bin/bash
        

        or as others said:

        ps h p $$ | awk '{ print $NF }'
        /bin/bash
        

        If you only need the shell name and not its executable full path:

        basename $( ps h p $$ | awk '{ print $NF }' )
        bash
        
  1. Thanks for this.
    But the last sentence…

    “Or better try out following if you have a bash shell:
    MYSHELL=$(ps -hp $$|awk ‘{echo $5}’)

    If you have a bash shell, couldn’t you just do
    MYSHELL=”bash”
    ?

  2. I noticed that, command such as; ps -$$, echo $SHELL, echo $0, printenv will print the current shell, but In case you have temperoraly changed your shell, these commands will not show the changes, unless you log out and log In. Whereas, cat /etc/passwd file will show the change mmediately without loging out.

    For ex: At the current shell (ex-/bin/bash), if you type ‘chsh’ and then enter /bin/tcsh, it will change the shell to ‘tcsh’, but the cmds mentioned above will still be reporting the old shell that is /bin/bash, unless you logout. But, /et/passwd file will show the changes immediately.

    1. Hi John,

      ps -p $$

      and

      echo $0

      always return the current shell, even from a sub-shell because they print the current process.

      The /etc/passwd file is simply a file and then it does not store the current user shell but the login shell. In your example, you are using the Change login shell (chsh) to actually change the shell automatically launched at your login, and this is why the change is immediately visible in the /etc/passwd file.

    2. I verified the output and it seems what you have said is totally opposite.. i.e, when u change the shell the changes are updated in ps -$$, echo $shell, echo $0 , and printenv. But not /etc/passwd file

  3. This is not 100% accurate.

    • echo $SHELL and other similar methods print the name the current shell is called in the current environment.
    • sh –version, on the other hand, prints the version of the default shell
    • If you combine them, you can spawn another process, running the same shell as the parent, and ask it what is its version.

    Example (note how my shell is given the name “csh”, but it is really a tcsh):

       [[email protected] ~]$ echo $SHELL
        /bin/csh
       [[email protected] ~]$ sh --version
        GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
        Copyright (C) 2005 Free Software Foundation, Inc.
       [[email protected] ~]$ $SHELL --version
        tcsh 6.14.00 (Astron) 2005-03-25 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,color,filec
    
  4. Hi, I have a problem which cannot be solved with the methods mentioned above. I need to find out within a bash script from which shell it was executed. If I use the methods above, I of course get always bash, since the executed script is a bash script. Does anybody have an idea?
    Cheers, Jim

  5. Which of the following command displays your login shell in Bash shell
    a) $SHELL b) echo$ Bash c) echo$ 0 d) $0

  6. Which of the following command displays your login shell in Bash shell
    a)$ SHELL
    b)echo $Bash
    c)echo$) 0
    d) $ 0

  7. When `echo $SHELL` does work for you, try `echo $$ $SHELL`. Due to $$ representing the {pid} column it will append the pid to the output; include a space and it becomes, more, human readable.

  8. Old question, but here is an answer. You need to get the current process’s parent id, and give that as the argument to ‘ps -p’. You can do it like this:

    ps -p `ps -o ppid –no-headers -p $$`

    If you want to convince yourself that you are getting the right answer, you can see the parent process id in a more human readable form with:

    ps -O ppid $$

    N.B. Lower case letter ‘o’ in the first command, upper case letter ‘O’ in the second one.

  9. When I was learning the Unix shell using bash, I learned that you can just type in the name of the shell and the output will tell you. For example, when I type “bash” it prints out “bash-3.2” which tells that I am using the bash shell.

Comments are closed.