Last active
October 21, 2024 09:24
-
-
Save ilg-ul/383869cbb01f61a51c4d to your computer and use it in GitHub Desktop.
BASH recommended initialisation sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}")" | |
# ============================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BASH recommended initialisation sequence
This snippet makes
bash
scripts more robust, by no longer ignoring errors. It should be inserted at the beginning of allbash
scripts.The explanations were copied from the
bash
manual page./usr/bin/env
The
env
is preferred (instead of directly starting thebash
), 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 theIFS
special variable. That is,$*
is equivalent to$1c$2c...
, where c is the first character of the value of theIFS
variable. IfIFS
is unset, the parameters are separated by spaces. IfIFS
is null, the parameters are joined without intervening separators.${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 theIFS
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 theIFS
special variable.IFS
as a delimiter, and splits the results of the other expansions into words on these characters. IfIFS
is unset, or its value is exactly<space><tab><newline>
, the default, then any sequence ofIFS
characters serves to delimit words. IfIFS
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 ofIFS
(anIFS
whitespace character). Any character inIFS
that is notIFS
whitespace, along with any adjacentIFS
whitespace characters, delimits a field. A sequence ofIFS
whitespace characters is also treated as a delimiter. If the value ofIFS
is null, no word splitting occurs.-W
option is considered. The string is first split using the characters in theIFS
special variable as delimiters.read
: The characters inIFS
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*
.