Functions to deal with errors in bash errcatch: set a trap on error to do bash-trace and exit errallow: undo errcatch errexit: exit and print stack trace bash-trace: print stack trace (best bash stack trace ever) the err* functions depend on bash-trace. For scripts without any nested function calls, this is usually sufficient: set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR Cleanup commands or functions can be appended to the ERR trap after a semicolon within the single quotes. Related: see my bash script template repo, at https://iankelling.org/git. Please email me if you have a patches, bugs, feedback, or republish this somewhere else: Ian Kelling .