5

I am trying to jump between zsh and bash.

By default, I have zsh as my shell, I can know this by typing:

echo $SHELL and I get /bin/zsh

However, I want to open Bash, so I type /bin/bash; I assume I am in bash now, but if I echo $SHELL I still get /bin/zsh

What's wrong, please?

4

SHELL is an environment variable that is passed from bash to zsh when you call zsh. SHELL is not one of the Parameters Set By The Shell in zsh, so its value remains intact.

bash$ SHELL=turtle zsh
zsh$ echo $SHELL
turtle 

For indications that you're in a zsh shell, try:

echo $ZSH_NAME
echo $0

The SHELL variable is traditionally set by the login program, "as specified by the password database". (Copied from What sets the $SHELL environment variable?)

  • I'd welcome better indications of a zsh shell; a possible list includes $module_path and $ZSH_VERSION, although in theory other shells could set those. – Jeff Schaller Jun 4 at 20:31
  • 1
    Also for consideration: ps -ocomm= -p $$ – Jeff Schaller Jun 4 at 20:33
  • thank you, but how did $SHELL get assigned in the first place? I install zsh just recently – Jack Thomson Jun 4 at 21:29
  • Great point, @Jack. See the update; your passwd entry likely still points to /bin/bash; you may try (or request the change) to set it to zsh. – Jeff Schaller Jun 4 at 22:36
2

Many shells set a version variable on starting.

bash use BASH_VERSION and zsh use ZSH_VERSION.

Setting both variables to a known value will reliably detect which shell was started:

BASH_VERSION=notbash ZSH_VERSION=notzsh sh -c 'echo "$BASH_VERSION $ZSH_VERSION"'

will print notbash 5.3.1 if sh is zsh or 5.0.2(2)-release notzsh if sh is bash.

Sadly ksh segfaults on using KSH_VERSION with a string value.

  • maybe echo $0 is a bit easier than ZSH_VERSION, BASH_VERSION. but I like the idea, didn't about those variables – Jack Thomson Jun 4 at 21:32
  • 1
    The value of $0 could be modified at will, for example: sh -c 'echo "$0"' MyNameOfShell and by several other methods. @JackThomson – Isaac Jun 4 at 21:34
  • fair point, indeed – Jack Thomson Jun 4 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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