Can bash show a function's definition?

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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" 

回答1:

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.



回答2:

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

declare -f foobar 


回答3:

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

with foobar being the function name.



回答4:

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!