Can bash show a function's definition?

前端 未结 4 2024
野的像风
野的像风 2020-12-22 15:21

Is there a way to view a bash function\'s definition in bash?

For example, say I defined the function foobar

function foobar {
    echo          


        
相关标签:
4条回答
  • 2020-12-22 15:39
    set | sed -n '/^foobar ()/,/^}/p'
    

    This basically prints the lines from your set command starting with the function name foobar () and ending with }

    0 讨论(0)
  • 2020-12-22 15:42

    Use type. If foobar is e.g. defined in your ~/.profile:

    $ type foobar
    foobar is a function
    foobar {
        echo "I'm foobar"
    }
    

    This does find out what foobar was, and if it was defined as a function it calls declare -f as explained by pmohandras.

    To print out just the body of the function (i.e. the code) use sed:

    type foobar | sed '1,3d;$d'
    
    0 讨论(0)
  • 2020-12-22 15:53

    You can display the definition of a function in bash using declare. For example:

    declare -f foobar
    
    0 讨论(0)
  • 2020-12-22 15:58
    set | grep -A999 '^foobar ()' | grep -m1 -B999 '^}'
    

    with foobar being the function name.

    0 讨论(0)
提交回复
热议问题