The two sides of the pipe operator run in parallel. This is done by running the two sides in separate processes. (It was the only way to do it in the original Unix shell, and it's still the only way to do it in modern shells.) Consider a snippet like
testb=left | testb=right
echo $testb
In zsh, the right-hand side of a pipe operator runs in the original process, and the left-hand side runs in a subshell. A subshell is normally implemented through a separate process; even if the shell optimizes the execution and doesn't use a separate process, it ensures that any modification of the shell state (variables, redirection, etc.) are confined to the subshell, for consistency. Thus the snippet above prints right
.
(Other shells may run the right-hand side of the pipe in a subshell as well. Amongst the common shells, only zsh and ATT ksh run the right-hand side in the original context. The left-hand side always runs in a subshell.)
If you want to pipe data without creating a subshell, you can use a process substitution. A process substitution creates a pipe in a subshell and gives control over whether the original context is connected to the the input side or the output side of the pipe.
test "testa" > >(cat)
(Note the space between >
and >(…)
: >>(cat)
would be parsed as a process substitution in append mode, which would pass a path to the pipe as a second argument to test
. Here we want to redirect the output of the function call to the pipe.)