This is an excerpt from my book: “Moving to zsh.” At the MacAdmins Conference Campfire session I received quite a few questions regarding this, so I thought it would be helpful information. You can get a lot more detailed information on “Moving to zsh” in the book!
Calls to the POSIX sh /bin/sh
in macOS are handled by /bin/bash
in sh compatibility mode. You can verify this by asking sh for its version:
% sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.
If Apple plans to eventually remove the bash binary, they need to have a replacement which handles sh.
Enter dash
Probably not coincidentally, Apple added the dash shell in Catalina. The Debian Almquist Shell (dash) is a minimal implementation of the Posix sh standard and commonly used on other UNIX and Unix-like systems as a stand-in for sh.
Apple also added dash (but not zsh) to the Recovery system in macOS Catalina. While sh is still interpreted by bash in both the Recovery system and the regular system, this is a strong indicator that Apple eventually wants to use dash as the interpreter for sh scripts.
When your scripts which use the #!/bin/sh
shebang strictly follow the POSIX sh standard, you should experience no problems when Apple switches to ‘dash-as-sh.’
Tripping over bashisms
However, there are some quirks of the current ‘bash-as-sh’ implementation in macOS that you need to be aware of. When bash stands in as sh, it will nevertheless continue to interpret ’bashisms’—language features available in bash but not sh—without errors.
For example, consider the following script shtest.sh
:
#!/bin/sh
if [[ $(true) == $(true) ]]; then
echo "still good"
else
echo "nothing is true"
fi
This script declares the #!/bin/sh
shebang and it will work fine on macOS with bash-as-sh.
% shtest.sh
still good
However, when you try to run it with zsh-as-sh or dash-as-sh, it will fail.
You can make dash interpret the script instead of bash by switching the shebang to #!/bin/dash
. But macOS Catalina has another, new mechanism available. In Catalina, the symbolic link at /var/select/sh
determines which shell stands in as sh. By default the link points to /bin/bash
:
% readlink /var/select/sh
/bin/bash
When you change this link to either /bin/zsh
or /bin/dash
, the respective other shell binary will stand in as sh.
Switch the sh stand-in to dash with:
% sudo ln -sf /bin/dash /var/select/sh
And then run the script again:
% ./shtest.sh
./shtest.sh: 3 ./shtest.sh: [[: not found
nothing is true
When interpreted with dash instead of bash, the same script will fail. This is because dash is much stricter than bash in following the sh standard. Since dash is designed as a minimal implementation of the sh standard, it has to be stricter. The double brackets [[ … ]]
are a ‘bashism,’ or a feature only available in bash and other, later shells such as ksh and zsh.
Even though zsh also interprets most of these bashisms, zsh in sh compatibility mode is also stricter than bash and will error.
You can switch back to the default bash-as-sh with:
% sudo ln -sf /bin/bash /var/select/sh
Since macOS has been using bash-as-sh for a long time, there may be many such bashisms lurking in your sh scripts. You can change the above symbolic link to test your scripts with dash-as-sh.
Some common ‘bashisms’ are:
- double square brackets
[[ ... ]]
- here documents and strings (
<<<
and<<
operators) - double equals operator
==
for tests
Shellcheck to the rescue
You can also use the shellcheck
tool to detect bashisms in your sh scripts:
% shellcheck shtest.sh
In shtest.sh line 3:
if [[ $(true) == $(true) ]]; then
^----------------------^ SC2039: In POSIX sh, [[ ]] is undefined.
For more information:
https://www.shellcheck.net/wiki/SC2039 -- In POSIX sh, [[ ]] is undefined.
When you change the double square brackets for single square brackets, then you get this:
% shellcheck shtest.sh
In shtest.sh line 3:
if [ "$(true)" == "$(true)" ]; then
^-- SC2039: In POSIX sh, == in place of = is undefined.
For more information:
https://www.shellcheck.net/wiki/SC2039 -- In POSIX sh, == in place of = is undefined.
- shellcheck: website, GitHub
- Scripting OS X: Install shellcheck binary on macOS
Conclusion
In Catalina Apple started warning us about the eventual demise of bash from macOS. Converting your existing bash scripts and workflows to zsh, sh, or bash v5 is an important first step. But you also need to consider that the behavior of sh scripts will change when Apple replaces the sh interpreter.