Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Last active November 8, 2024 20:05
Show Gist options
  • Save akostadinov/33bb2606afe1b334169dfbf202991d36 to your computer and use it in GitHub Desktop.
Save akostadinov/33bb2606afe1b334169dfbf202991d36 to your computer and use it in GitHub Desktop.
Get stack trace in Bash shell script/program.
# LICENSE: MIT, wtfpl or whatever OSS license you like
function get_stack () {
STACK=""
local i message="${1:-""}"
local stack_size=${#FUNCNAME[@]}
# to avoid noise we start with 1 to skip the get_stack function
for (( i=1; i<$stack_size; 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
STACK+=$'\n'" at: "$func" "$src" "$linen
done
STACK="${message}${STACK}"
}
@RobertKrawitz
Copy link

RobertKrawitz commented Mar 3, 2024

I did it that way so that higher level code can more conveniently capture the output; it can also redirect it to stderr. Either way works, certainly.

I find the arguments to be invaluable for debugging. I have a very complex script that itself provides an API, and there's a lot of control flow complexity. Ideally it would be written in Go or something, but none of those languages provide the convenience of shell scripting for running other commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment