"Linde, Evan" <eli...@okstate.edu> writes:
> In a loop constructed like `... | while read ...`, changes to 
> variables declared outside the loop only have a loop local
> scope, unlike other "while" or "for" loops.

Yeah, that's a gotcha.  But it's a general feature of *pipelines*,
documented in

       Each command in a pipeline is executed as a separate process (i.e.,  in
       a  subshell).  See COMMAND EXECUTION ENVIRONMENT for a description of a
       subshell environment.  If the lastpipe  option  is  enabled  using  the
       shopt builtin (see the description of shopt below), the last element of
       a pipeline may be run by the shell process.

To circumvent that, I've sometimes done things like

    exec 3<( ... command to generate stuff ... )
    while read VAR <&3; do ... commands to process stuff ... ; done
    exec 3<-

You may be able to condense that to

    {
    while read VAR <&3; do ... commands to process stuff ... ; done
    } <( ... command to generate stuff ... )

Changing {...} to (...) won't work, because (...) again executes things
in a subshell.

(But don't trust that code without checking it!)

Dale

Reply via email to