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

Re: Pipe input consumed where I didn't expect it to



On 2016/12/05, at 6:13, Jesper Nygårds <jesper.nygards@xxxxxxxxx> wrote:

> However, if I call it like this:
> % for c in 1 2 3; do echo $c; done | while read f; do ./streamtest; done
> 
> I get the output:
> x 2
> x 3
> 
> Obviously my script consumes line 2 and 3, and prints them. Why is this the
> case? I naively thought that the outer read (of f) on the command line
> would be fed from the pipe, and then call "streamtest" without a pipe
> leading into the script.

When the while loop starts, 'read f' is called once and consumes 1. Since
the read succeeds, now streamtest is called, which consumes rest of the
input (2 3) and exists. Now 'read f' is called again but fails, so the
loop ends.



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