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
fi

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

IFS=$'\n\t'

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 comment