POSIX sh and shebangs
Posted on
As I keep moving towards POSIX, I’m on the process of migrating all my Bash scripts to POSIX sh.
As I dropped [[, arrays and other Bashisms, I was left staring at the first line of every
script, wondering what to do: what is the POSIX sh equivalent of #!/usr/bin/env bash? I already
knew that POSIX says nothing about shebangs, and that the portable way to call a POSIX sh script is sh
script.sh, but I didn’t know what to do with that first line.
What I had previously was:
Obviously, the $BASH_SOURCE would be gone, and I would have to adapt some of my scripts to
not rely on the script location. The -E and -o pipefail options were also gone, and
would be replaced by nothing.
I converted all of them to:
-
when running via
./script.sh, if the system has an executable at/bin/sh, it will be used to run the script; -
when running via
sh script.sh, the sh options aren’t ignored as previously.
TIL.