Skip to content

Instantly share code, notes, and snippets.

@ashayh
Forked from ryo1kato/bash functrace
Created July 7, 2017 22:01
Show Gist options
  • Save ashayh/c48f4c97a0f1db605b762e0ac18fa718 to your computer and use it in GitHub Desktop.
Save ashayh/c48f4c97a0f1db605b762e0ac18fa718 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