bash 中是否可以在函数退出时调用某些命令。我的意思是:
function foo
{
# something like this maybe?
trap "echo \"exit function foo\"" EXIT
# do something
}
foo
我想打印出退出函数 foo。
最佳答案
是的,你可以捕获 RETURN
:
$ function foo() {
> trap "echo finished" RETURN
> echo "doing some things"
> }
$ foo
会显示
doing some things
finished
来自 man bash
对 trap
内建函数的描述:
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.
关于退出函数时的 Bash 陷阱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45862044/