BASH recommended initialisation sequence

Writing scripts to automate repetitive tasks is very common, but it is not commonly known that bash defaults are not very robust.

In order to make bash better process error cases, it is necessary to set several options, one of the most notable being to complain about using variables without assigning them values.

The recommended code to do this is

#!/usr/bin/env bash

if [[ ${DEBUG} != "" ]]; then
set -x

set -o errexit
set -o pipefail
set -o nounset


The up-to-date version of the small snippet is available from Gist, together with some more explanations.


About Liviu Ionescu (ilg)
Hi! My name is Liviu Ionescu (ilg, ilegeul or eunete for colleagues and friends) and I’m a senior IT engineer. Or should I say a real programmer?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: