I want to get function caller name in shell script sometime, in bash it works with ${FUNCNAME[1]}
${FUNCNAME[1]}
is a (caller name)
${FUNCNAME[0]}
is c (current name)
but it not work in zsh
ie i want to know which function call me in function c
function a(){
c
}
function b(){
c
}
function c(){
#if a call me; then...
#if b call me; then...
}
Generic solution
KSH_ARRAYS
) or 1 (default)zsh
andbash
Edge case
The difference between
bash
andzsh
is that when calling this function from asource
d file,bash
will saysource
whilezsh
will say the name of the file being sourced.The function call stack is in the variable
$funcstack[]
.So in
c
the calling function (a
orb
) is$funcstack[2]
or perhaps more conveniently$funcstack[-1]
.