Skip to content

Instantly share code, notes, and snippets.

@gturi
Last active May 7, 2023 09:03
Show Gist options
  • Save gturi/0f8330d664a968585176be7e036c3637 to your computer and use it in GitHub Desktop.
Save gturi/0f8330d664a968585176be7e036c3637 to your computer and use it in GitHub Desktop.
Template for less error prone bash scripts

Bash script template

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
export SHELLOPTS

cd "$(dirname "$0")"

SCRIPT_DIR="$(pwd)"

Notes

  • set -o errexit:
    • when a command fails, bash exits instead of continuing with the rest of the script
  • set -o nounset:
    • the script will fail when accessing an unset variable
    • to access a variable that may or may not have been set, use "${VARNAME-}" instead of "$VARNAME"
  • set -o pipefail:
    • a pipeline command is treated as failed, when one of the commands in the pipeline fail
  • cd "$(dirname "$0")":
    • change to the script's directory
  • if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi:
    • enables debug mode by running TRACE=1 ./script.sh
  • remember to redirect error logs to stderr
    • i.e. echo 'Something unexpected happened' >&2
  • export SHELLOPTS:
    • makes sub scripts inherit set options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment