bug-bash
[Top][All Lists]
Advanced

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

Re: Pipe using subshells, variables not saved


From: Chet Ramey
Subject: Re: Pipe using subshells, variables not saved
Date: Mon, 6 Mar 2017 15:33:14 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.7.1

On 3/6/17 2:44 PM, Misaki wrote:
> 
> On Sat, Mar 4, 2017 at 10:12 AM, Chet Ramey <chet.ramey@case.edu
> <mailto:chet.ramey@case.edu>> wrote:
> 
>     On 3/4/17 1:04 AM, Misaki wrote:
>     > Variables are not saved when using a pipe because both sides are in a
>     > subshell environment.
> 
>     If you want this to happen, enable the `lastpipe' shell option.
> 
> 
> Thank you. My standpoint is 'whatever makes things easier for new users', I
> definitely don't have detailed knowledge of design decisions. But I see
> this:
> http://unix.stackexchange.com/questions/143958/in-bash-read-after-a-pipe-is-not-setting-values
> 
> Someone says that not using a subshell for pipelines is POSIX-compliant.

Posix says you need to assume that each element of a pipeline is run in
a subshell, but not depend on it.  That's non-specific enough to make
both behaviors conformant.

> Even if bash does use a subshell, is there a reason why lastpipe also
> requires job execution to be off (with 'set -m')?

Yes.  Consider what happens when you type ^Z. What do you do with all
the processes in the pipeline, especially the final one, and how do you
continue the pipeline?  That doesn't even begin to take into account
what you need to do with process groups to keep them synchronized. There
are some fragile solutions there, like forking a copy of the shell when
it sees ^Z and fixing everything up (pipes, file descriptors, process
groups), but that's more trouble than the implementation is worth.

That said, if anyone would like to take a run at it, I'd be glad to
look at the result.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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