Is it possible to get the function name in function body? [duplicate]
Try ${FUNCNAME[0]}
. This array contains the current call stack. To quote the man page:
FUNCNAME
An array variable containing the names of all shell functions
currently in the execution call stack. The element with index 0
is the name of any currently-executing shell function. The bot‐
tom-most element is "main". This variable exists only when a
shell function is executing. Assignments to FUNCNAME have no
effect and return an error status. If FUNCNAME is unset, it
loses its special properties, even if it is subsequently reset.
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:
$ ./sample foo bar $ cat sample #!/bin/bash foo() { echo ${FUNCNAME[ 0 ]} # prints 'foo' echo ${FUNCNAME[ 1 ]} # prints 'bar' } bar() { foo; } bar