Created
July 16, 2020 14:41
-
-
Save configurator/b7101308b8a20d86d24c6ab8ef63fac5 to your computer and use it in GitHub Desktop.
Bash header for safer code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e # exit on error | |
set -u # error on undefined variable | |
set -o posix # errors in $() expressions propagate outwards | |
set -o pipefail # errors in a pipeline will propagate outwards | |
# set -e | |
# Any command returning a nonzero code will exit the script | |
# If you need to run a command that may file, use it either in `if` expressions, or part of an `or` expression such as `command || true` | |
# set -u | |
# Using undefined variables such as $THING will fail thanks | |
# If you still need to use env variables, e.g. to know if they were passed in, use ${THING:-}, which means $THING if it's provided, and the empty string if it isn't | |
# set -o posix | |
# Normally, if you do something like `var=$(false)`, this will not propagate the error | |
# in POSIX mode, the error propagates | |
# note that this changes a bunch of other behaviors, mostly minor changes | |
# set -o pipefail | |
# Normally, if part of a pipeline fail but the last command succeeds, the pipeline counts as a success | |
# `syntaxerror | true` will not result in script failure | |
# pipefail fixes that so it will fail the script rather than just plodding along quietly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment