1

This question already has an answer here:

i did not yet found a solution to this. Anyone a hint?

i sometimes write bash functions in my shell scripts and i love to have my scripts being verbose, not just for debugging. so sometimes i would like to display the "name" of a called bash function as a "variable" in my scripts.

what i did sometimes is setting just a regular variable containing the function name. like this:

test ()
{
    funcName=test
    echo "function running..."
    echo "\$0 is : $0"
    echo "function name is : $funcName"
}

but that is kinda stupid.

Is there something better?

marked as duplicate by Stephen Kitt, EightBitTony, don_crissti, Jeff Schaller, dr01 Apr 1 '16 at 12:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In this instance it's easy to find the dupe when you know the answer, it's hard to phrase the search to find the answer when you only know your question... – Stephen Kitt Apr 1 '16 at 12:05
1

Sometimes it's enough to read man bash:

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 bottom-most element (the one with the highest index) is "main". This variable exists only when a shell function is executing. Assignments to FUNC- NAME have no effect and return an error status. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset.

Example usage:

#!/usr/bin/env bash

func()
{
    echo I am inside "$FUNCNAME"
}

foo()
{
    echo I am inside "$FUNCNAME"
}

func
foo
  • 2
    sometimes people do that and still do not find it. – Axel Werner Apr 1 '16 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.