Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

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.