Search code examples
bashmacosshellunixzsh

Which shell I am using in mac


Default shell in my mac was bash.
I have tried to change it into ZSH by command chsh -s /bin/zsh.

Now when I am trying to check the shell type, I am getting different responses.

COMMAND-1

input : echo $SHELL
output : /bin/zsh

COMMAND-2

input : ps $o
output : 7655 ttys002 0:00.03 -bash

COMMAND-3

input : ps -p $$ | awk '$1 == PP {print $4}' PP=$$
output : -bash

I am not sure which shell I am using. Do I need to do something additional to change my shell into ZSH.


Solution

  • To see what shell is currently running - which may or may not be your default shell - use:

    # Prints something like '/bin/ksh' or '-zsh'
    # See bottom section if you always need the full path.
    ps -o comm= $$
    

    The above assumes that the running shell is a POSIX-compatible shell. If the running shell is PowerShell, replace $$ with $PID, which will tell you the full path even if PowerShell is also the default shell. If you use
    (Get-Process -Id $PID).Path instead, you'll get the full path with symlinks resolved, if any.

    To see what shell is your default shell, run:

    echo $SHELL
    

    If the currently running shell is PowerShell: $env:SHELL


    If you need to know the full path of the currently running shell:

    If the current shell was launched directly by Terminal.app (or iTerm2), it is a login shell launched via the login utility, which causes the current shell process to self-report its binary abstractly as -<binary-filename>, e.g. -zsh; that is, you don't get the full path of the binary underlying the shell process.

    If always obtaining the full path is required - e.g. if you want to distinguish the system Bash /bin/bash from a later version installed via Homebrew - you can use the following command line:

    (bin="$(ps -o comm= $$)"; expr "$bin" : '\(-\)' >/dev/null && bin="$(ps -o command= $PPID | grep -Eo ' SHELL=[^ ]+' | cut -f 2- -d =)"; [ -n "$bin" ] && echo "$bin" || echo "$SHELL")