Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: substituted command won't inherit stdin in pipeline

2019-11-13 12:52:20 +0300, Oğuz:
> On all sh implementations I have, this command
>     echo foo | echo "$(cat)"
> prints 'foo', except for zsh, it hangs instead. From that I gather cat
> doesn't inherit echo's stdin, and it waits for input.

Not exactly what happens in that the expansions in the arguments
of the commands in the pipeline are performed in the parent,
from left to right, not in the processes that run the
individualy pipe components (also note that the right-most pipe
component is run in the current shell anyway, like in ksh93,
unlike in bash).


   echo bar | { echo foo | echo "$(cat)"; }

would output "bar".


   n=0; echo $((++n)) | echo $((++n))

outputs 2.

There are advantages and drawbacks in the way zsh works, but
it's too late to change it.

Here, to be compatible with the bash and zsh approaches, you'd

   echo foo | (echo "$(cat)")

Or to get the same as bash -O lastpipe:

   n=0; echo foo | { echo "$((++n))$(cat)"; }; echo "$n"


Messages sorted by: Reverse Date, Date, Thread, Author