Bash陷阱退出功能

前端之家收集整理的这篇文章主要介绍了Bash陷阱退出功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
函数退出时,bash中是否有可能调用某些命令.我的意思是:
  1. function foo
  2. {
  3. # something like this maybe?
  4. trap "echo \"exit function foo\"" EXIT
  5.  
  6. # do something
  7. }
  8.  
  9. foo

我希望打印退出函数foo.

@H_403_6@
是的,你可以捕获RETURN:
  1. $function foo() {
  2. > trap "echo finished" RETURN
  3. > echo "doing some things"
  4. > }
  5. $foo

显示

  1. doing some things
  2. 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相关文章