Consider the following commands:
exit > /dev/null
exit | cat
On a few shells (ksh, bash, (d)ash), the behavior is the same: The first command causes the shell to quit immediately, while the second one has no visible behavior.
I concluded that the first command did not involve a fork(2), while the second command involved two (one to execute exit
, the other execve(2)s to cat
).
I looked at the POSIX specification section 2.14, but didn't find anything explicitly stating this.
Is it specified by POSIX that the some commands must not fork(2) while some others must? Is spawning a subshell for the first command acceptable by the standard?
I am aware that ( exit )
should never exit the current shell, because the parentheses spawns a sub-shell that actually executes the exit
command, which means the sub-shell will exit immediately. However, I am unsure about redirections and piping, since there's no parenthesis here.
I am asking this question because in a recent course lab, we're told to implement a minimal Unix shell. There are many "optional features" that we can implement, for additional points. One of those "optional features" is combined redirection and pipelining. We had a few different implementations and I believe some are closer to the specified behavior of POSIX than others, and therefore I'd like to know how the actual behavior is specified.
exit
; it's the subshell from piping that matters here. See second bullet point in Stephane's answer. – muru Apr 26 '19 at 15:57