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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70

8 Answers8

188

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.

Ben
  • 60,438
  • 111
  • 314
  • 488
adl
  • 15,627
  • 6
  • 51
  • 65
  • 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](https://skitch.com/kaluvchris/8p7ua/terminal-zsh-79x24-1) – Juanito Fatas Mar 28 '12 at 19:48
  • @juanitofatas You ran the script under bash, so it correctly reported it was running under bash. – Gilles 'SO- stop being evil' Mar 28 '12 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. – Gilles 'SO- stop being evil' Mar 28 '12 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. – Rob Bednark Aug 03 '12 at 15:09
  • Fails on `set -u`: unbound variable – timotheecour Sep 23 '20 at 20:35
  • While possibly useful, this will fail if `export BASH_VERSION` is executed prior to transitioning from bash to zsh. Likewise `export ZSH_VERSION` when transitioning from zsh to bash. Even without an export scenario, depending on an environmental variable is risky, since it could be easily modified or `unset` prior to the shell check. Conceivably you could use `typeset -r ZSH_VERSION`and `typeset -r BASH_VERSION` ... tho it's kludgy at best, difficult to implement correctly, and probably poor practice. – ShpielMeister May 02 '21 at 02:27
  • zsh doesn't set $ZSH_VERSION anymore. It does set a var called $ZSH though. – agconti Jun 22 '21 at 17:23
  • @agconti latest development version of Zsh still sets $ZSH_VERSION and it is unlikely they will remove that variable that is used in many places by Zsh itself. – adl Jun 23 '21 at 15:42
  • @adl if you grep through the zsh repo you can see that it's not set: https://github.com/ohmyzsh/ohmyzsh/search?q=ZSH_VERSION. This checks out empirically too as neither myself or any of my team members have it set in their shell. Why do you think it's still set? – agconti Jun 23 '21 at 16:18
  • 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 https://github.com/zsh-users/zsh/blob/master/Src/params.c#L938 and then used all over the place. – adl Jun 25 '21 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.

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
2xsaiko
  • 1,043
  • 10
  • 14
  • 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 05 '14 at 01: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 '17 at 06:04
  • 5
    This doesn't work if you're in a function, where $0 will return the function name. – Jools Oct 24 '17 at 09:55
  • 1
    This doesn’t work when called by a script either, it gives the name of the script – Tom Kelly Apr 14 '20 at 05: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``` – blizzrdof77 Sep 15 '20 at 08: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.

Community
  • 1
  • 1
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • 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. – Michael Mior Sep 11 '16 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 '17 at 19:07
  • 7
    As expected, the accepted solution only offers rhetorical admonishment and chastisement rather than meaningful solutions. Everyone wants [adl](https://stackoverflow.com/users/27835/adl)'s [authoritative answer](https://stackoverflow.com/a/9911082/2809027) instead, which provides several general-purpose snippets addressing the actual question. – Cecil Curry Nov 01 '18 at 02: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. – Gilles 'SO- stop being evil' Nov 01 '18 at 07:34
7

You can simply try

 echo $SHELL
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
  • 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 '21 at 04: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
timotheecour
  • 3,104
  • 3
  • 26
  • 29
3

An alternative, might not work for all shells.

for x in $(ps -p $$)
do
  ans=$x
done
echo $ans
pizza
  • 7,296
  • 1
  • 25
  • 22
  • 2
    `ps -p $$ --no-headers -o cmd` is a bit better. – forivall Oct 04 '13 at 19:59
  • 1
    Except for when it doesn't work: `ps: unknown option -- no-headers` – Jools Oct 24 '17 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 '20 at 02: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                                                                                                                                               
agathodaimon
  • 154
  • 1
  • 5
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
djangofan
  • 28,471
  • 61
  • 196
  • 289