My understanding of running commands in subshells is that the current shell is forked, and the subshell then does a further fork
and exec
on the desired commands.
I see strange behaviour when running the following in both bash and zsh:
$ ( sleep 5 && sleep 6 )
When running in bash:
$ echo $$
6410
$ ( sleep 5 && sleep 6 )
ps
gives the following:
CMD: sleep 5
PID: 6590
PPID: 6589
and then:
CMD: sleep 6
PID: 6616
PPID: 6589
This all makes sense to me. sleep 5
and sleep 6
both have the same parent (presumably the subshell).
However, in zsh, I get the following:
$ echo $$
1987
$ ( sleep 5 && sleep 6 )
ps
gives the following:
CMD: sleep 5
PID: 7576
PPID: 7575
and then:
CMD: sleep 6
PID: 7575
PPID: 1987
I can't make head or tail of why sleep 5
has the process used to run sleep 6
as its parent and the process used to run sleep 6
has the original shell as its parent?
zsh
seems to meet the specification for the( )
operator. Thesleep
processes do not run in the first-level shell (1987 in your example), they do indeed run as subprocesses. I don't think there's a requirement that the processes have a common parent.