In macOS Monterey, a script with a shebang (e.g. #!/bin/sh) is not executed by the indicated interpreter via zsh. For example, the following script will yields the below outcome:


#!/usr/bin/env bash
echo $SHELL


% chmod u+x my_script
% my_script

I want my scripts to run with bash. I would prefer that the shebang be respected than entirely change my shell. How can I ensure this happens?

Related questions that don't answer mine:

  1. The shebang doesn't work if you're sourcing the script with '.': I'm not
  2. Run the script like: bash my_script: I want it to respect the shebang, not change my behaviour so I need to check each script before running it with the right shell.
  3. Change your shell to bash: I wish to keep the standard macOS shell.

2 Answers 2


Your script is being run with bash. $SHELL is set once to the path to your login shell when it starts, not a dynamically changing value for the shell currently executing the command that prints $SHELL.


One big difference is that bash by default sets the variable SHELL, but zsh doesn't. Therefore in some implementations, the system configuration (for instance via /etc/zshenv) set SHELL as environment variable to the zsh binary; AFIK, Apple is doing this in MacOS.

bash would set SHELL to the path of its binary, but it does this only if there is no environment variable of this name. You can verify this (while in zsh) with

SHELL=foo bash -c 'printenv SHELL'

(unset SHELL; bash -c 'echo $SHELL')

The first line will print foo, while the second one will print something like /bin/bash.

Hence, if you want bash to get SHELL to be set, you have to unset it in your zsh before running your bash script. However, since the cases where this variable is actually needed are rare, I don't think this is worth the effort.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .