Stephane CHAZELAS <stephane.chaze...@gmail.com> wrote:

> > > > $ ./sh -o posix -c 'a=1; { a=2; } < /dev/null; echo "$a"'
> > > > 1
> > > >
> > > > Again 2 required by POSIX.
> > 
> > Do you have a pointer to the POSIX text that forbids a subshell in this 
> > case 
> > when stdin is redirected?
> [...]
>
> I don't know if you'll find some text that *explicitly* forbids
> it to run in a subshell, but you won't find text that *allows*
> it (just like you won't see text that allows { a=2; } alone to
> run in a subshell environment)

Whether this is relevant with POSIX mainly depends on whether you expect a 
specific behavior with this command:

          lastline=
          while read line
          do

                  lastline=$line
          done < /etc/passwd
          echo "lastline=$lastline"


The Bourne Shell always created a subshell for a complex command with I/O 
redirection, but it is on my TODO list for useful changes in future.

Just as a note: "posh" and the Bourne Shell both print an empty line here.


> On the other hand, you'll find text that explicitely allows it
> to run in a subshell environment in:
>
> { a=2; } | cmd
>
> cmd | { a=2; }
>
> (even though for the latter, many shells don't run it in a
> subshell environment).

This is a general problem: POSIX does not mention the way pipes are set up and 
probably never will as this yould enforce many shell to be rewritten.

The historic Bourne Shell did create many sub shells with pipes because this 
could be done with less code and RAM was rare in the 1970s.

bosh has been rewritten completely with repect to pipe handling and this is one 
of the reasons why it is noticeably faster than the historic Bourne Shell.

This is because the new pipe handling in bosh makes the rightmost process the 
foreground process which allows:

        echo bla | read var

to behave as people would expect.

Jörg

-- 
 EMail:jo...@schily.net                    (home) Jörg Schilling D-13353 Berlin
    joerg.schill...@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/
 URL: http://cdrecord.org/private/ http://sf.net/projects/schilytools/files/'

Reply via email to