This question already has an answer here:
In BASH, is it possible to get the function name in function body? Taking following codes as example, I want to print the function name "Test" in its body, but "$0" seems to refer to the script name instead of the function name. So how to get the function name?
#!/bin/bash
function Test
{
if [ $# -lt 1 ]
then
# how to get the function name here?
echo "$0 num" 1>&2
exit 1
fi
local num="${1}"
echo "${num}"
}
# the correct function
Test 100
# missing argument, the function should exit with error
Test
exit 0
The name of the function is in
${FUNCNAME[ 0 ]}
FUNCNAME is an array containing all the names of the functions in the call stack, so:Try
${FUNCNAME[0]}
. This array contains the current call stack. To quote the man page: