当函数退出时,bash中是否有可能调用某些命令.我的意思是:
- function foo
- {
- # something like this maybe?
- trap "echo \"exit function foo\"" EXIT
- # do something
- }
- foo
是的,你可以捕获RETURN:
- $function foo() {
- > trap "echo finished" RETURN
- > echo "doing some things"
- > }
- $foo
会显示
- doing some things
- finished
从man bash对内置陷阱的描述:
If a sigspec is RETURN,the command arg is executed each time a shell function or a script executed with the . or source builtins finishes executing.