$ cat test1.sh
#!/bin/bash
setsid sleep 100
'test1.sh'shell脚本将不立即退出。
$ cat test2.sh
#!/bin/bash
setsid sleep 100 &
'test2.sh'shell脚本将立即退出。
有人能为我解释一下吗?非常感谢。
答案 0 :(得分:0)
你的第一个脚本等待setsid sleep 100
完成然后返回(换句话说,它将在睡眠结束后返回,这里100秒),但第二个脚本不等待命令完成,原因&
将命令置于后台并立即返回。引用man bash
:
If a command is terminated by the control operator &, the shell executes the
command in the background in a subshell. The shell does not wait for the command
to finish, and the return status is 0.
另请注意,&
是一个控制运算符,它具有更高的优先级。希望这有帮助!