Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[BASH] Print stacktrace
# Original:
# The difference is that this func outputs stacktrace in reverse order (from top level to lower ones)
function print_stacktrace () {
local i
local stack_size=${#FUNCNAME[@]}
echo "Stacktrace:"
# skip this function and "MAIN non_file_source:0"
for (( i=$stack_size-1; i>=1; i-- )); do
local func="${FUNCNAME[$i]}"
[ x$func = x ] && func=MAIN
local linen="${BASH_LINENO[$(( i - 1 ))]}"
local src="${BASH_SOURCE[$i]}"
[ x"$src" = x ] && src=non_file_source
echo -e "\n at $func $src:$linen"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment