How to determine function name from inside a function

前端 未结 5 882
梦如初夏
梦如初夏 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 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
    

提交回复
热议问题