Bash陷阱退出功能

前端之家收集整理的这篇文章主要介绍了Bash陷阱退出功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
函数退出时,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对内置陷阱的描述:

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/384404.html

猜你在找的Bash相关文章