开发者

Can bash show a function's definition?

Is there a way to view a 开发者_开发知识库bash function's definition in bash?

For example, say I defined the function foobar

function foobar {
    echo "I'm foobar"
}

Is there any way to later get the code that foobar runs?

$ # non-working pseudocode
$ echo $foobar
echo "I'm foobar"


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'


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

declare -f foobar


set | sed -n '/^foobar ()/,/^}/p'

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


set | grep -A999 '^foobar ()' | grep -m1 -B999 '^}'

with foobar being the function name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜