Skip to content

Instantly share code, notes, and snippets.

@null-dev
Last active March 4, 2026 23:12
Show Gist options
  • Select an option

  • Save null-dev/c1c466e8a14f49fd3cfca0f61ee20a11 to your computer and use it in GitHub Desktop.

Select an option

Save null-dev/c1c466e8a14f49fd3cfca0f61ee20a11 to your computer and use it in GitHub Desktop.
Useful bash functions
# Exit script if uninitialised variable is used:
set -o nounset
# Print the line number of the caller, the contents of the line, and exit with code 1
err_panic() {
local msg="$1"
local line_num="${BASH_LINENO[0]}"
local source_file="${BASH_SOURCE[1]}"
local line_content=$(sed -n "${line_num}p" "$source_file" | sed 's/^[ \t]*//')
local red=$(tput setaf 1)
local bold=$(tput bold)
local reset=$(tput sgr0)
echo "${bold}${red}--- ERROR ---${reset}" >&2
echo "${bold}${msg:-"Runtime Error"}${reset}" >&2
echo "${bold}[$source_file:$line_num]${reset}" >&2
echo "${bold} ╰─${reset} $line_content" >&2
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment