Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created July 13, 2012 05:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryo1kato/3102982 to your computer and use it in GitHub Desktop.
Save ryo1kato/3102982 to your computer and use it in GitHub Desktop.
Backtrace for bash
#!/bin/bash
set -ue
bash_trace () {
typeset -i i=0
for func in "${FUNCNAME[@]}"
do
printf '%15s() %s:%d\n' \
"$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[$i]}"
let i++ || true
done
}
trap 'echo "Unexpected error at: line $LINENO"; bash_trace; exit' ERR EXIT
exit () {
trap - ERR EXIT
command exit "$@"
}
################
foo () { false; }
bar () { foo; }
baz () { bar; }
baz
# With set -e, you won't reach here
echo Zzz...
sleep 999
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment