zsh-users
 help / color / mirror / code / Atom feed
* Re: substituted command won't inherit stdin in pipeline
       [not found] <CAH7i3Lo57yAd==m7zF+8mtETqYDDSptmdzxF-2t_pu42fe9ztQ__41421.1000244663$1573638841$gmane$org@mail.gmail.com>
@ 2019-11-13 13:00 ` Stephane Chazelas
  2019-11-13 13:30   ` Oğuz
       [not found]   ` <CAH7i3LrwD4G0CJ3WvdWZsp5u-cSTQoGomSf6HHF1XG8NnA89Vw__34634.6754276723$1573691143$gmane$org@mail.gmail.com>
  0 siblings, 2 replies; 7+ messages in thread
From: Stephane Chazelas @ 2019-11-13 13:00 UTC (permalink / raw)
  To: Oğuz; +Cc: zsh-users

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: substituted command won't inherit stdin in pipeline
  2019-11-13 13:00 ` substituted command won't inherit stdin in pipeline Stephane Chazelas
@ 2019-11-13 13:30   ` Oğuz
       [not found]   ` <CAH7i3LrwD4G0CJ3WvdWZsp5u-cSTQoGomSf6HHF1XG8NnA89Vw__34634.6754276723$1573691143$gmane$org@mail.gmail.com>
  1 sibling, 0 replies; 7+ messages in thread
From: Oğuz @ 2019-11-13 13:30 UTC (permalink / raw)
  To: Oğuz, zsh-users

[-- 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?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: substituted command won't inherit stdin in pipeline
       [not found]   ` <CAH7i3LrwD4G0CJ3WvdWZsp5u-cSTQoGomSf6HHF1XG8NnA89Vw__34634.6754276723$1573691143$gmane$org@mail.gmail.com>
@ 2019-11-14  7:57     ` Stephane Chazelas
  2019-11-14  8:43       ` Oğuz
  0 siblings, 1 reply; 7+ messages in thread
From: Stephane Chazelas @ 2019-11-14  7:57 UTC (permalink / raw)
  To: Oğuz; +Cc: zsh-users

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: substituted command won't inherit stdin in pipeline
  2019-11-14  7:57     ` Stephane Chazelas
@ 2019-11-14  8:43       ` Oğuz
  2019-11-14  9:35         ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Oğuz @ 2019-11-14  8:43 UTC (permalink / raw)
  To: stephane.chazelas; +Cc: zsh-users

[-- 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
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: substituted command won't inherit stdin in pipeline
  2019-11-14  8:43       ` Oğuz
@ 2019-11-14  9:35         ` Peter Stephenson
  2019-11-14  9:38           ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2019-11-14  9:35 UTC (permalink / raw)
  To: zsh-users

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: substituted command won't inherit stdin in pipeline
  2019-11-14  9:35         ` Peter Stephenson
@ 2019-11-14  9:38           ` Roman Perepelitsa
  0 siblings, 0 replies; 7+ messages in thread
From: Roman Perepelitsa @ 2019-11-14  9:38 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* substituted command won't inherit stdin in pipeline
@ 2019-11-13  9:52 Oğuz
  0 siblings, 0 replies; 7+ messages in thread
From: Oğuz @ 2019-11-13  9:52 UTC (permalink / raw)
  To: zsh-users

[-- 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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-11-14  9:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAH7i3Lo57yAd==m7zF+8mtETqYDDSptmdzxF-2t_pu42fe9ztQ__41421.1000244663$1573638841$gmane$org@mail.gmail.com>
2019-11-13 13:00 ` substituted command won't inherit stdin in pipeline Stephane Chazelas
2019-11-13 13:30   ` Oğuz
     [not found]   ` <CAH7i3LrwD4G0CJ3WvdWZsp5u-cSTQoGomSf6HHF1XG8NnA89Vw__34634.6754276723$1573691143$gmane$org@mail.gmail.com>
2019-11-14  7:57     ` Stephane Chazelas
2019-11-14  8:43       ` Oğuz
2019-11-14  9:35         ` Peter Stephenson
2019-11-14  9:38           ` Roman Perepelitsa
2019-11-13  9:52 Oğuz

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).