Created
December 26, 2017 16:12
-
-
Save imkiva/bd7b183df4e7b931e364c15a327607db to your computer and use it in GitHub Desktop.
Get calling stack in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_stack() { | |
STACK="" | |
# to avoid noise we start with 1 to skip get_stack caller | |
local i | |
local stack_size=${#FUNCNAME[@]} | |
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'" "$func" "$src" "$linen | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment