Skip to content

Instantly share code, notes, and snippets.

@ilg-ul
Last active April 19, 2022 13:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ilg-ul/383869cbb01f61a51c4d to your computer and use it in GitHub Desktop.
Save ilg-ul/383869cbb01f61a51c4d to your computer and use it in GitHub Desktop.
BASH recommended initialisation sequence
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Safety settings (see https://gist.github.com/ilg-ul/383869cbb01f61a51c4d).
if [[ ! -z ${DEBUG} ]]
then
set ${DEBUG} # Activate the expand mode if DEBUG is anything but empty.
else
DEBUG=""
fi
set -o errexit # Exit if command failed.
set -o pipefail # Exit if pipe failed.
set -o nounset # Exit if variable not set.
# Remove the initial space and instead use '\n'.
IFS=$'\n\t'
# -----------------------------------------------------------------------------
# Identify the script location, to reach, for example, the helper scripts.
script_path="$0"
if [[ "${script_path}" != /* ]]
then
# Make relative path absolute.
script_path="$(pwd)/$0"
fi
script_name="$(basename "${script_path}")"
script_folder_path="$(dirname "${script_path}")"
script_folder_name="$(basename "${script_folder_path}")"
# =============================================================================
@ilg-ul
Copy link
Author

ilg-ul commented Nov 1, 2015

BASH recommended initialisation sequence

This snippet makes bash scripts more robust, by no longer ignoring errors. It should be inserted at the beginning of all bash scripts.

The explanations were copied from the bash manual page.

/usr/bin/env

The env is preferred (instead of directly starting the bash), because it automatically identifies the program in the PATH and sets the environment.

DEBUG

Define DEBUG=-x, to activate the expand mode, useful to debug scripts.

errexit (-e)

Exit immediately if a simple command exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or || list, or if the command's return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits.

pipefail

If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

nounset (-u)

Treat unset variables as an error when performing parameter expansion. If expansion is attempted on an unset variable, the shell prints an error message, and, if not interactive, exits with a non-zero status.

IFS

The Internal Field Separator is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.

  • $* expansion: Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, $* is equivalent to $1c$2c..., where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
  • array members: Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.
  • ${!prefix*} and ${!prefix@} Expand to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable.
  • word splitting: The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly <space><tab><newline>, the default, then any sequence of IFS characters serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.
  • completion: the string specified as the argument to the -W option is considered. The string is first split using the characters in the IFS special variable as delimiters.
  • read: The characters in IFS are used to split the line into words.

This script redefines IFS to remove the initial space and instead use '\n', for example as separator for joined words when expanding *.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment