≡ Menu

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

Asked by Chetan Joshi

Q. What is the best way to find out what shell I'm using. echo $SHELL is not so reliable. Please let me know any tiny command or trick.

A. Chetan, echo $SHELL should work. But 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 following command to find out what shell you are in:

ps -p $$

So what is $ argument passed to -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 $$
Output:

  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}')

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • Akram March 12, 2008, 11:51 am

    thanks this is what i looking for

  • Paul Harper June 30, 2008, 4:49 pm

    you can also use
    echo $0

    • PaulD February 14, 2013, 6:48 am

      This is the only method listed here that works for me (Mac OS X).

    • ania salve December 4, 2013, 10:39 am

      Thanks .. this was veryuse ful..echo $0

  • maki May 17, 2010, 4:22 pm

    -hp might not work. Also, echo?

    ps h -p $$ | awk ‘{print $5}’

  • ashwin June 8, 2010, 4:22 pm

    You can also find this out from et/passwd file :

    Command to run:
    # cat /etc/passwd

    • Abhisek August 10, 2012, 11:57 am

      Sure, does this give accurate result?

      • lexlythius September 5, 2012, 3:29 pm

        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
        
  • lee murray July 8, 2010, 11:58 am

    exactly what i was looking for. thanks

  • Suneelm July 21, 2010, 10:16 am

    Thanks ..

    • ujjal December 21, 2011, 12:29 pm

      you can also use
      ps | grep $$

  • gitterrost4 September 19, 2010, 7:42 am

    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”
    ?

    • Victor February 7, 2012, 1:09 pm

      LOL, just what I was thinking. ^^

  • akshay October 19, 2010, 11:37 am

    ps -p $$
    and
    echo $0

    thanks a lot!

  • john fuller October 27, 2010, 6:33 pm

    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.

    • Giuseppe Bertone August 30, 2011, 11:01 am

      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.

    • Raghavendra October 5, 2012, 3:22 pm

      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

  • san January 5, 2011, 5:53 pm

    you can also use

    ps -u

  • Tim Jowers February 24, 2011, 8:37 pm

    $ bash
    in
    [a148503@wuvra99a0219 logs]$ uname -a
    Linux wuvra99a0219 2.6.18-194.8.1.el5 #1 SMP Wed Jun 23 10:52:51 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

    $SHELL is the only thing remaining as ksh:

    [a148503@wuvra99a0219 logs]$ ps -$$
    PID TTY STAT TIME COMMAND
    18336 pts/13 S 0:00 bash
    [a148503@wuvra99a0219 logs]$ echo $SHELL
    /usr/bin/ksh
    [a148503@wuvra99a0219 logs]$ echo $0
    bash

  • cindy April 3, 2011, 12:48 am

    pwd.

  • CRP September 2, 2011, 7:51 pm

    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):

       [me@localhost ~]$ echo $SHELL
        /bin/csh
       [me@localhost ~]$ sh --version
        GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
        Copyright (C) 2005 Free Software Foundation, Inc.
       [me@localhost ~]$ $SHELL --version
        tcsh 6.14.00 (Astron) 2005-03-25 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,color,filec
    
    • Abhisek August 10, 2012, 12:01 pm

      So how to get the exact 100% accurate result?
      How to know the current shell I’m using?

  • Ajit June 6, 2012, 11:41 am

    Nice tricks.

  • jim August 8, 2012, 3:43 pm

    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

  • octy October 24, 2012, 6:38 pm

    Just a note: why use awk if
    ps -p $$ -o ucomm=
    does the trick and supported by most *nixes?

  • octy October 24, 2012, 6:40 pm

    Just a note: why use awk, if
    ps -p $$ -o ucomm=
    does the trick?

  • shubha December 16, 2013, 2:44 am

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

  • shubha December 16, 2013, 2:46 am

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

  • Skinnx86 February 22, 2015, 10:14 pm

    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.