Write Safe Shell Scripts
set -euf -o pipefail
set -e
If a command fails, set -e will make the whole script exit, instead of just resuming on the next line.
set -u
Treat unset variables as an error, and immediately exit.
set -f
Disable filename expansion (globbing) upon seeing *, ?, etc.
set -o pipefail
set -o pipefail causes a pipeline (for example, curl -s https://sipb.mit.edu/ grep foo) to produce a failure return code if any command errors.
In addition echo
should be avoided using printf
instead.
Read this insightful post and the POSIX recommendation.