[-- Attachment #1: Type: text/plain, Size: 453 bytes --] 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. This works though echo foo | { echo "$(cat)"; } and it doesn't make any sense, both should work as per the standard if I'm not mistaken. Is this a bug, or documented behavior? --- $ zsh --version zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
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).
So:
echo bar | { echo foo | echo "$(cat)"; }
would output "bar".
And:
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
do:
echo foo | (echo "$(cat)")
Or to get the same as bash -O lastpipe:
n=0; echo foo | { echo "$((++n))$(cat)"; }; echo "$n"
--
Stephane
[-- Attachment #1: Type: text/plain, Size: 789 bytes --] On Wed, Nov 13, 2019 at 4:00 PM Stephane Chazelas < stephane.chazelas@gmail.com> wrote: > 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). > [...] Now I'm confused. Is this what standard says, or what zsh does?
2019-11-13 16:30:06 +0300, Oğuz: [...] > > 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). Which was meant to be (sorry for the gibberish): > > Not exactly. What happens is 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). > Now I'm confused. Is this what standard says, or what zsh does? It's what zsh does. IIRC, POSIX leaves it unspecified. It also leaves unspecified which pipeline component if any may be run in the current shell process. -- Stephane
[-- Attachment #1: Type: text/plain, Size: 984 bytes --] On Thu, Nov 14, 2019 at 10:57 AM Stephane Chazelas < stephane.chazelas@gmail.com> wrote: [...] > It's what zsh does. > Oh, okay then. > IIRC, POSIX leaves it unspecified. > > It also leaves unspecified which pipeline component if any may > be run in the current shell process. > The standard says > Additionally, each command of a multi-command pipeline is in a subshell > environment; as an extension, however, any or all commands in a pipeline > may be executed in the current environment. I thought this means each component of a pipeline should be executed in a subshell, but as an *extension*, implementations may prefer executing some of them in the current environment. Seems like I was wrong. Btw, I installed a more recent version (5.7.1), and in the DESCRIPTION section of manual it says > It does not provide compatibility with POSIX or other shells in its default > operating mode... Now this explains everything. Sorry if I wasted your time > -- > Stephane >
> On 14 November 2019 at 08:43 Oğuz <oguzismailuysal@gmail.com> wrote:
> Now this explains everything. Sorry if I wasted your time
This stuff's tricky, and very few people have got as much of a handle on it
as Stephane, so I don't think you should consider it as time wasting.
pws
On Thu, Nov 14, 2019 at 10:36 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> > On 14 November 2019 at 08:43 Oğuz <oguzismailuysal@gmail.com> wrote:
> > Now this explains everything. Sorry if I wasted your time
>
> This stuff's tricky, and very few people have got as much of a handle on it
> as Stephane, so I don't think you should consider it as time wasting.
I've learned something new from this exchange. It was a good question
and a great answer by Stephane.
Roman.