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 Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: