Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Last active February 15, 2024 05:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oldratlee/902ad9a398affca37bfcfab64612e7d1 to your computer and use it in GitHub Desktop.
Save oldratlee/902ad9a398affca37bfcfab64612e7d1 to your computer and use it in GitHub Desktop.
error trap in bash
#!/bin/bash
set -eEuo pipefail
# https://stackoverflow.com/questions/6928946/mysterious-lineno-in-bash-trap-err
# https://stackoverflow.com/questions/64786/error-handling-in-bash
# https://stackoverflow.com/questions/24398691/how-to-get-the-real-line-number-of-a-failing-bash-command
# https://unix.stackexchange.com/questions/39623/trap-err-and-echoing-the-error-line
# https://unix.stackexchange.com/questions/462156/how-do-i-find-the-line-number-in-bash-when-an-error-occured
# https://unix.stackexchange.com/questions/365113/how-to-avoid-error-message-during-the-execution-of-a-bash-script
# https://shapeshed.com/unix-exit-codes/#how-to-suppress-exit-statuses
# https://stackoverflow.com/questions/30078281/raise-error-in-a-bash-script/50265513#50265513
# https://github.com/codeforester/base/blob/master/lib/stdlib.sh
__error_trapper() {
local parent_lineno="$1"
local code="$2"
local commands="$3"
echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR
#false hello world
#command_not_existed hello hell
func1() {
echo func1_in_1
command_not_existed arg1_1 arg1_2
func2
echo func1_in_2
}
func2() {
echo func2_in_1
command_not_existed arg2_1 arg2_2
echo func2_in_2
}
func1
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment