Strict Mode Bash Script Skeleton

Start scripts with strict mode settings so unset variables, failing commands, and pipe errors stop execution instead of being silently ignored.

BashCli Tooling

Code

bashsnippet
17 lines
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

log() {
  printf '%s [%s] %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$1" "$2"
}

if [[ $# -lt 1 ]]; then
  log ERROR "usage: $(basename "$0") <target>"
  exit 64
fi

log INFO "starting against $1"
"${SCRIPT_DIR}/process.sh" "$1"

How it works

Strict mode (set -euo pipefail) makes failures loud — an unset variable or a failing pipeline stage aborts the script instead of being swallowed.

The work is delegated to readonly and log, chained so each stage’s output feeds the next.

A helper function (log) keeps the repeated logic out of the main flow.

Using it

The script self-documents: the help branch prints $(basename.

The key statements, annotated

readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
log ERROR "usage: $(basename "$0") <target>"

Interactive Tools for This Topic

FAQ

Does the script stop on the first error?
Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
Which external commands does the script rely on?
`readonly`, `log` must be available on the PATH environment variable; everything else is shell built-in syntax.