bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash 5.1 heredoc pipes problematic, shopt needed


From: Greg Wooledge
Subject: Re: bash 5.1 heredoc pipes problematic, shopt needed
Date: Mon, 25 Apr 2022 16:04:01 -0400

On Mon, Apr 25, 2022 at 11:43:31PM +0400, Alexey via Bug reports for the GNU 
Bourne Again SHell wrote:
> Annex: with reading to buffer there is some problem: if I want to read first
> part to variable and rest of pipe pass to external program... Bash could be
> an additional pipe layer for next program, but this looks like some
> overhead.

Bash's "read" is good for that.  It doesn't use buffers, so it won't
"read ahead" and lose part of the input the way e.g. head(1) might.

The problem here is getting the variable to persist.  The naive way one
might write something like this is:

  foo | { read -r myvar; bar; }

which will indeed read one line from the stream, stick it in a variable,
and then allow the rest of the stream to be handled by bar.  However, the
variable is set in a subshell, so it's not accessible after the pipeline
terminates.

Instead, you need a construct like this:

  { read -r myvar; bar; } < <(foo)

That runs the "read" in the main shell process instead of a subshell, so
the variable remains set after "bar" finishes.

None of this has anything to do with here documents or here strings, or
the changes made in bash 5.1.

(And no, I'm not going to sidetrack into "lastpipe".  You can look into
that if you wish.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]