我有一个简单的脚本
trap 'echo exit' EXIT
while true; do sleep 1; done
它在不同的 shell 中表现不同:
$ bash tst.sh
^Cexit
$ dash tst.sh
^C
$ zsh tst.sh
^C
$ sh tst.sh
^Cexit
所以我不确定它应该如何运作以及它是否被指定。
最佳答案
EXIT
trap 在每个 shell 中的工作方式不同。几个例子:
通过显式调用
exit
恢复默认行为. 我建议你实际捕捉信号然后退出,它应该是可移植的
在大多数 shell 中:
$ cat trap
trap 'echo exit; exit' INT TERM # and other signals
while true; do sleep 1; done
$ bash trap
^Cexit
$ dash trap
^Cexit
$ zsh trap
^Cexit
$ ksh trap
^Cexit
$ mksh trap
^Cexit
$ busybox sh trap
^Cexit
关于shell - 如果收到 SIGINT 或 SIGTERM,是否需要执行 trap EXIT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27012762/