Using #!/path/to/strict-bash
sometimes results in strict-bash
being ignored and bash
itself interpreting the script.
According to https://stackoverflow.com/questions/9988125/shebang-pointing-to-script-also-having-shebang-is-effectively-ignored, MacOS (and other BSD-derived unices) can’t use a script as a shebang interpreter. This is extra-painful on MacOS as the bash available by default is ancient, and many scripts won't work on it.
The default shell on MacOS is ZSH, which somehow avoids this issue. Using #!/usr/bin/env strict-bash
avoids it as well. However, when using this with Nix, those options are less viable. Nix doesn’t like env
and instead rewrites to an absolute path to the interpreter, which breaks this behavior. Also, Nix shells tend to use Bash, not ZSH. I.e., Nix is where you start seeing this issue.
One way to avoid this would be to make strict-bash
an actual binary.
In the meantime, a workaround (adapted from the Stack Overflow link above) is
#!/usr/bin/env bash
exec -o "$0" strict-bash <<EOM
... content to be executed ...
EOM
Of course, once you’re doing that, it's just as easy to use
#!/usr/bin/env bash
source strict-mode.bash
... content to be executed ...