Interview Prep/Shell Scripting/Best practices and rapid-fire commands

What does a production-ready script template look like?

AdvancedConceptBest practices and rapid-fire commands
Answer
#!/usr/bin/env bash
# deploy.sh: roll out a release to the web tier. Usage: deploy.sh -e ENV VERSION
set -Eeuo pipefail
shopt -s inherit_errexit nullglob

readonly SCRIPT_NAME=${0##*/}
log()  { printf '%s %s [%s] %s\n' "$(date -u +%FT%TZ)" "$1" "$SCRIPT_NAME" "${*:2}" >&2; }
die()  { log ERROR "$*"; exit 1; }
usage(){ sed -n '2p' "$0" | sed 's/^# //'; }

cleanup() { [[ -n ${tmpdir:-} ]] && rm -rf -- "$tmpdir"; }
trap cleanup EXIT
trap 'log ERROR "line $LINENO: $BASH_COMMAND"' ERR

main() {
  local env=""
  while getopts ':e:h' o; do
    case $o in e) env=$OPTARG ;; h) usage; exit 0 ;; *) usage >&2; exit 2 ;; esac
  done
  shift $((OPTIND - 1))
  [[ $env =~ ^(dev|staging|prod)$ ]] || die "-e must be dev, staging or prod"
  local version=${1:?version required}
  tmpdir=$(mktemp -d)
  log INFO "deploying $version to $env"
}
main "$@"
All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Best practices and rapid-fire commands