Why is bash unavailable even though installed

bashhomebrewterminalzsh

My goal is to run a script, where the shebang line causes bash to be used. Somehow running bash always runs zsh for me, even though I installed bash 5 using Homebrew and Mac (10.15) comes with bash 3.

I would still like zsh to be the default shell.

My goal is to run scripts with bash, and the shebang lines with bash give me zsh, as shown by the syntax that the script recognizes. The use of -c below is just for ease of illustration (as compared to a file with shebang).

Here are my attempts to access bash and the content of /etc/shells

 
    $ /usr/local/opt/bash/bin/bash -c "echo $SHELL"
    /bin/zsh
    $ whereis bash
    /bin/bash
    $ /bin/bash -c "echo $SHELL"
    /bin/zsh
    $ /bin/bash -c 'echo $SHELL'
    /bin/zsh
    $ /usr/local/bin/bash -c "echo $SHELL"
    /bin/zsh
    $ /usr/local/bin/bash -c 'echo $SHELL'
    /bin/zsh
    $ cat /etc/shells
    # List of acceptable shells for chpass(1).
    # Ftpd will not allow users to connect who are not using
    # one of these shells.
    /usr/local/bin/bash
    /bin/bash
    /bin/csh
    /bin/dash
    /bin/ksh
    /bin/sh
    /bin/tcsh
    /bin/zsh
    $ /usr/bin/env bash -c "echo $SHELL"
    /bin/zsh
    $ /bin/bash  -c "ps -p $$"
    PID TTY           TIME CMD
    6775 9 ttys011    0:00.31 -zsh
    $/usr/local/opt/bash/bin/bash -c "ps -p $$"
    PID TTY           TIME CMD
    69799 ttys001    0:00.38 -zsh
    $ /usr/local/bin/bash -c 'ps -p $$'
    PID TTY           TIME CMD
    69985 ttys001    0:00.01 ps -p 69985
    $ /bin/bash -c "echo $0"
    -zsh
    $ /usr/local/opt/bash/bin/bash -c "echo $0"
    -zsh

Also, I created the following file and ran it directly

#!/usr/bin/env bash
# With /bin/bash on the shebang we get the same output
ps -p $$
echo $SHELL
echo $0
echo bash $BASH_VERSION
echo zsh $ZSH_VERSION

The result is:

$ ./bashtest.sh
  PID TTY           TIME CMD
70188 ttys000    0:00.00 /bin/bash ./bashtest.sh
/bin/zsh
./bashtest.sh
bash 3.2.57(1)-release
zsh

Best Answer

Your examples do not show that the shell you are running is zsh.

What they show is the default shell is zsh and the ps one shows you ran bash from inside a zsh shell.

A way to see the current shell is echo $0 from https://askubuntu.com/questions/590899/how-do-i-check-which-shell-i-am-using However this only works in some cases. It does work if you call bash from zsh as you are doing. It does not work on the non POSIX shells I use e.g. fish and xonsh

$SHELL is the default shell and not what you are running (and that is zsh as you want) login man pages e.g. http://man.openbsd.org/login.1 (or run man login on macOS) $SHELL is set by the login program . However as noted in the man page shells might do other things and also you can (not by default) run a non login shell in Terminal.app and I think that might also have $SHELL set

For the process the $$ is the pid of what the command line sees e.g. trhe shell that runs the bash line you gave.

To see this

  1. Start bash e.g. bash
  2. In the bash shell now run ps -p $$ you get something like
    ~ bash
     bash-5.0$ ps -p $$
      PID TTY           TIME CMD
    28098 ttys000    0:00.01 bash
    bash-5.0$ exit

~ is my zsh prompt. bash is bash 5.0 installled via macports