bug-bash
[Top][All Lists]
Advanced

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

Re: Scope change in loops with "read" built-in


From: Dale R. Worley
Subject: Re: Scope change in loops with "read" built-in
Date: Thu, 04 Apr 2024 20:39:51 -0400

"Linde, Evan" <elinde@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

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