How to determine function name from inside a function

前端 未结 5 876
梦如初夏
梦如初夏 2020-12-02 04:24

If I have a Bash script like:

#!/bin/bash

f() {
  # echo function name, \"f\" in this case
}

Is there any way to do this? This could be us

相关标签:
5条回答
  • 2020-12-02 04:58

    You can use ${FUNCNAME[0]} in bash to get the function name.

    0 讨论(0)
  • 2020-12-02 05:03

    I use ${FUNCNAME[0]} to print current function name

    0 讨论(0)
  • 2020-12-02 05:08

    From the Bash Reference Manual:

    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 FUNCNAME have no effect and return an error status. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset.

    This variable can be used with BASH_LINENO and BASH_SOURCE. Each element of FUNCNAME has corresponding elements in BASH_LINENO and BASH_SOURCE to describe the call stack. For instance, ${FUNCNAME[$i]} was called from the file ${BASH_SOURCE[$i+1]} at line number ${BASH_LINENO[$i]}. The caller builtin displays the current call stack using this information.

    When bash arrays are accessed without an index the first element of the array will be returned, so $FUNCNAME will work in simple cases to provide the name of the immediately current function, but it also contains all other functions in the call stack. For example:

    # in a file "foobar"
    function foo {
        echo foo
        echo "In function $FUNCNAME: FUNCNAME=${FUNCNAME[*]}" >&2
    }
    
    function foobar {
        echo "$(foo)bar"
        echo "In function $FUNCNAME: FUNCNAME=${FUNCNAME[*]}" >&2
    }
    
    foobar
    

    Will output:

    $ bash foobar
    In function foo: FUNCNAME=foo foobar main
    foobar
    In function foobar: FUNCNAME=foobar main
    
    0 讨论(0)
  • 2020-12-02 05:15

    The simplest way to get the function name (from inside a function) is dependent on which shell you are using:

    Zsh version

    someFunctionName() {
       echo $funcstack[1]
    }
    

    Bash version

    someFunctionName() {
       echo ${FUNCNAME[0]}
    }
    

    Both

    someFunctionName() {
      currentShell=$(ps -p $$ | awk "NR==2" | awk '{ print $4 }' | tr -d '-')
      if [[ $currentShell == 'bash' ]]; then
        echo ${FUNCNAME[0]}
      elif [[ $currentShell == 'zsh' ]]; then
        echo $funcstack[1]
      fi
    }
    
    0 讨论(0)
  • 2020-12-02 05:16

    Another example:

    # in a file "foobar"
    foo() {
        echo "$FUNCNAME fuction begins"
    }
    
    foobar() {
        echo "$FUNCNAME fuction begins"
    }
    
    echo 'begin main'
    foo
    foobar
    echo 'end main'
    

    Will output:

    begin main
    foo fuction begins
    foobar fuction begins
    end main
    
    0 讨论(0)
提交回复
热议问题