5 shell bash background-process subshell
说我有这个:
set -e;
(
docker stop notifier-server
docker rm -f notifier-server
exit 1 # explicitly exit with non-zero
) &
wait;
echo 'we are here now'
Run Code Online (Sandbox Code Playgroud)
即使子shell以非零值退出,我们是否总是会到达回声线?我认为是的,因为它是一个后端进程/子shell?
看待这个问题的正确方法是什么?
您的代码不会终止当前 shell 会话,因为没有返回非零退出状态。启动后台作业的调用 shell 中的结果始终为零。
如果您使用了wait "$!"or wait -n(“等待下一个作业完成”),那么 shell 会话将终止,因为wait将返回它等待的作业的退出状态,该状态不为零。
见于.help waitbash