85

I've a question on how to tell which shell the user is using. Suppose a script that if the user is using zsh, then put PATH to his .zshrc and if using bash should put in .bashrc. And set rvmrc accordingly.

#!/usr/bin/env bash
export PATH='/usr/local/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

I've tried the following but it does not work : (

if [[ $0 == "bash" ]]; then
  export PATH='/usr/local/bin:$PATH' >> ~/.bashrc
elif [[ $0 == "zsh" ]]; then
  export PATH='/usr/local/bin:$PATH' >> ~/.zshrc
fi

# ... more commands ...

if [[ $0 == "bash" ]]; then
  [[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.bashrc
  source ~/.bashrc
elif [[ $0 == "zsh" ]]; then
  [[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.zshrc
  source ~/.zshrc
fi
4

8 Answers 8

191

If the shell is Zsh, the variable $ZSH_VERSION is defined. Likewise for Bash and $BASH_VERSION.

if [ -n "$ZSH_VERSION" ]; then
   # assume Zsh
elif [ -n "$BASH_VERSION" ]; then
   # assume Bash
else
   # assume something else
fi

However, these variables only tell you which shell is being used to run the above code. So you would have to source this fragment in the user's shell.

As an alternative, you could use the $SHELL environment variable (which should contain absolute path to the user's preferred shell) and guess the shell from the value of that variable:

case $SHELL in
*/zsh) 
   # assume Zsh
   ;;
*/bash)
   # assume Bash
   ;;
*)
   # assume something else
esac

Of course the above will fail when /bin/sh is a symlink to /bin/bash.

If you want to rely on $SHELL, it is safer to actually execute some code:

if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
   # assume Zsh
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
   # assume Bash
else
   # assume something else
fi

This last suggestion can be run from a script regardless of which shell is used to run the script.

10
  • Hi Thank you for your answer. I've tried your last method. While I resides in zsh, I ran your script, but it still says I am using bash. I took a photo: screenshot Mar 28, 2012 at 19:48
  • @juanitofatas You ran the script under bash, so it correctly reported it was running under bash. Mar 28, 2012 at 20:09
  • 4
    @juanitofatas You ran zsh in a terminal. From that interactive zsh, you ran bash to interpret a script. In that script, you tested which shell was executed the script, and the answer was bash. Mar 28, 2012 at 20:29
  • @adl, the last code chunk is missing a "$" in front of SHELL. I tried editing it, but StackOverflow edits require at least 6 characters to be changed. Aug 3, 2012 at 15:09
  • 1
    @agconti It's normal for ohmyzsh (a set of configuration files, not zsh itself) to not set that variable. That variable is set in the Zsh source code github.com/zsh-users/zsh/blob/master/Src/params.c#L938 and then used all over the place.
    – adl
    Jun 25, 2021 at 11:57
56

Just do echo $0 it says -zsh if it's zsh and -bash if it's bash

EDIT: Sometimes it returns -zsh and sometimes zsh and the same with bash, idk why.

5
  • 2
    This doesn't work for all shells, but is still my go-to method any time I sit down at a CLI -- it works more times than not. Shells such as Fish will not return anything for echo $0
    – kmatheny
    Dec 5, 2014 at 1:59
  • 5
    The -zsh or -bash is used to indicate a login shell. If it doesn't have the - infront of the name, then the user is using a non-login shell
    – smac89
    Jun 29, 2017 at 6:04
  • 5
    This doesn't work if you're in a function, where $0 will return the function name.
    – Jools
    Oct 24, 2017 at 9:55
  • 1
    This doesn’t work when called by a script either, it gives the name of the script
    – Tom Kelly
    Apr 14, 2020 at 5:23
  • detected_shell=$(echo "${0//-/}") ... You can test it like this: if [[ -f "${HOME}/.${detected_shell}rc" ]]; then echo "Found ${HOME}/.${detected_shell}rc"; fi Sep 15, 2020 at 8:27
41

A word of warning: the question you seem to have asked, the question you meant to ask, and the question you should have asked are three different things.

“Which shell the user is using” is ambiguous. Your attempt looks like you're trying to determine which shell is executing your script. That's always going to be whatever you put in the #! line of the script, unless you meant your users to edit that script, so this isn't useful to you.

What you meant to ask, I think, is what the user's favorite shell is. This can't be determined fully reliably, but you can cover most cases. Check the SHELL environment variable. If it contains fish, zsh, bash, ksh or tcsh, the user's favorite shell is probably that shell. However, this is the wrong question for your problem.

Files like .bashrc, .zshrc, .cshrc and so on are shell initialization files. They are not the right place to define environment variables. An environment variable defined there would only be available in a terminal where the user launched that shell and not in programs started from a GUI. The definition would also override any customization the user may have done in a subsession.

The right place to define an environment variable is in a session startup file. This is mostly unrelated to the user's choice of shell. Unfortunately, there's no single place to define environment variables. On a lot of systems, ~/.profile will work, but this is not universal. See https://unix.stackexchange.com/questions/4621/correctly-setting-environment and the other posts I link to there for a longer discussion.

4
  • 14
    "That's always going to be whatever you put in the #! line of the script" - That's not true since a user could easily just pass the script filename as a parameter to any shell. Sep 11, 2016 at 22:00
  • 2
    @MichaelMior: And the user probably will pass the script filename as a parameter to some random shell, if the script is not marked executable. Depending on your installation process, this may or may not be the case.
    – Kevin
    Dec 12, 2017 at 19:07
  • 7
    As expected, the accepted solution only offers rhetorical admonishment and chastisement rather than meaningful solutions. Everyone wants adl's authoritative answer instead, which provides several general-purpose snippets addressing the actual question. Nov 1, 2018 at 2:21
  • @CecilCurry I think my answer was accepted because it answers what the asker meant to ask. adl's answer is a more detailed treatment of the title, but that's often the wrong question to ask. Nov 1, 2018 at 7:34
7

You can simply try

 echo $SHELL
1
  • 1
    This fails. In Zsh, if I open a bash subshell and do this, it still says /bin/zsh, which is obviously not the shell.
    – iconoclast
    Nov 21, 2021 at 4:46
5

the other answers fail with set -u

  if [ ! -z ${ZSH_VERSION+x} ]; then
    echo "this is zsh"
    echo ${(%):-%x}
  elif [ ! -z ${BASH_VERSION+x} ]; then
    echo "this is bash"
    echo $BASH_SOURCE
  else
    echo "not recognized"
  fi
1
  • 2
    This succeeds if I open a bash subshell from zsh, unlike echo $SHELL
    – iconoclast
    Nov 21, 2021 at 4:47
3

An alternative, might not work for all shells.

for x in $(ps -p $$)
do
  ans=$x
done
echo $ans
3
  • 2
    ps -p $$ --no-headers -o cmd is a bit better.
    – forivall
    Oct 4, 2013 at 19:59
  • 1
    Except for when it doesn't work: ps: unknown option -- no-headers
    – Jools
    Oct 24, 2017 at 10:00
  • ps -f -o cmd= -p "$$" | sed '1{/^$/N;s/^\n//;t r;:r;/^\[/{:l;$s/^\[\(.*\)\]$/\1/;t;N;b l}}' should be robust and complete. (The sed deals with stripping -f fallback annotations & non–POSIX-compliant ps implementations that still write header lines when all header text fields are null.) It looks like the cmd format name isn't standard, though, so you could also try ps -o comm= -p "$$" | sed '1{/^$/N;s/^\n//}' if that fails.
    – bb010g
    Jan 21, 2020 at 2:05
1

Myself having a similar problem, settled for:

_shell="$(ps -p $$ --no-headers -o comm=)"                                                                                                       
if [[ $_shell == "zsh" ]]; then                                                                                                                  
    read -q -s "?Do it?: "                                                                                                                    
fi                                                                                                                                               
elif [[ $_shell == "bash" || $_shell == "sh" ]]; then                                                                                              
    read -n 1 -s -p "Do it [y/n] "                                                                                                            
fi                                                                                                                                               
1

Here is how I am doing it based on a previous answer from Gilles :

if [ -n "$ZSH_VERSION" ]; then
  SHELL_PROFILE="$HOME/.zprofile"
else
  SHELL_PROFILE="$HOME/.bash_profile"
fi
echo "export VAR1=whatever" >> $SHELL_PROFILE
echo "INFO: Refreshing your shell profile: $SHELL_PROFILE"
if [ -n "$ZSH_VERSION" ]; then
  exec zsh --login
else
  source $SHELL_PROFILE
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.