Why is bash unavailable even though installed?

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

Solution 1:

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

Solution 2:

Variables in "" get expanded by the running shell, zsh in your case. Use '' instead.

Solution 3:

$SHELL is documented in the bash manal as being your login shell, not the currently running shell. I don't see that variable documented in the zsh manual at all.

There's nothing particular magical about it.

My login shell is fish, and that's what bash sets it to if the variable is unset:

env --unset=SHELL bash -c 'echo $SHELL'    # => /usr/local/bin/fish

If you have a script that is supposed to run under either bash or zsh, then you can check if certain shell-specific variables exist:

echo "this is a bash or zsh script"
if [[ -n $BASH_VERSION ]]; then
    echo "I'm running in $BASH $BASH_VERSION"
elif [[ -n $ZSH_VERSION ]]; then
    echo "I'm running in $ZSH_NAME $ZSH_VERSION"
else
    echo "I'm running under some other POSIX-type shell"
    ps -ef | grep "$$"
fi

I have a bunch of different shells installed:

$ zsh shell.sh
this is a bash or zsh script
I'm running in zsh 5.8

$ bash shell.sh
this is a bash or zsh script
I'm running in /usr/local/bin/bash 5.0.18(1)-release

$ sh shell.sh
this is a bash or zsh script
I'm running in /bin/sh 3.2.57(1)-release

$ dash shell.sh
this is a bash or zsh script
shell.sh: 2: [[: not found
shell.sh: 4: [[: not found
I'm running under some other shell
  502 31943 31595   0  7:03PM ttys006    0:00.01 dash shell.sh
    0 31944 31943   0  7:03PM ttys006    0:00.00 ps -ef
  502 31945 31943   0  7:03PM ttys006    0:00.00 grep 31943

$ ksh shell.sh
this is a bash or zsh script
I'm running under some other shell
  502 31952 31595   0  7:04PM ttys006    0:00.01 ksh shell.sh
    0 31953 31952   0  7:04PM ttys006    0:00.00 ps -ef
  502 31954 31952   0  7:04PM ttys006    0:00.00 grep 31952