How I start my Bash scripts
#!/usr/bin/env bash set -Eeuxo pipefail # ...
This is the interpreter directive (or shebang).
Older Bash shebangs used to look something like
bash executable might not be in
/bin on all systems.
Portability isn’t guarenteed.
The drawbacks of using this approach seem to be:
- It’s hard to pass additional parameters to the intepreter. Not all versions of
envseem to support the
-Sargument needed for this. I can’t recall having to do this for Bash, but have run into issues with ts-node (archive) before.
- Just like
bashmay not be in
envmay not be in
/usr/bin. I’ve never run into this in the wild though.
set -Eeuxo pipefail
ecommand disables the
e- Stop executing the script when a command fails. Vital.
u- Error when trying to access unset variables.
x- Print each command before executing it, including expanding the value of arguments.
o pipefail- Bash won’t propogate non-zero exit codes to the end of a pipeline. This turns that on.
For more, see Safer bash scripts with ‘set -euxo pipefail’ · vaneyckt.io (archive).