Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Last active June 6, 2022 17:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavxyz/bba16a257010848e534ac5c9e0e56172 to your computer and use it in GitHub Desktop.
Save stavxyz/bba16a257010848e534ac5c9e0e56172 to your computer and use it in GitHub Desktop.
BASHISM - If you are a civilized person, you'll start all your bash scripts like this
#!/usr/bin/env bash
# Undefined variables are errors.
set -euoE pipefail
errcho ()
{
printf "%s\n" "$@" 1>&2
}
errxit ()
{
errcho "$@"
# shellcheck disable=SC2119
errdie
}
# shellcheck disable=SC2120
errdie() {
if [ -n "${1:-}" ]; then
_errmsg="⏩ Error at line ${1} of ${BASH_SOURCE:-${0:-}}"
if [ -n "${2:-}" ]; then
_errmsg="${_errmsg} on command '${2}'"
if [ -n "${3:-}" ]; then
_errmsg="${_errmsg} in function '${3}'"
fi
fi
fi
errcho "${_errmsg}"
echo "🛀" # perform any cleanup
exit 1
}
intdie() {
errcho "🍿 script discontinued."
echo "🛀" # perform any cleanup
exit 1
}
trap 'errdie ${LINENO} ${BASH_COMMAND:-} ${FUNCNAME:-}' ERR
trap 'intdie ${LINENO} ${BASH_COMMAND:-} ${FUNCNAME:-}' SIGHUP SIGINT SIGTERM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment