by catern · 10y ago · 0 comments
Three real defensive bash programming tips are:
- Quote all uses of variables
- set -o nounset
- set -o errexit
And many others can be found in and around http://mywiki.wooledge.org/BashFAQ
Using pipefail, if any program in a pipeline fails (i.e. exit code != 0), then the exit code for the pipeline will be != 0.
E.g. pipefail can be useful to ensure `curl does-not-exist-aaaaaaa.com | wc -c` doesn't exit with exit code 0..!
set -o nounset -o pipefail -o errexit set -eu -o pipefail #!/...
set -eu
# ... Main part of your script ...The latter makes it clearer exactly what features are being enabled, and it's a bit of a false economy to try and "shorten" the script like this.