Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 19 Mar 2011 20:30:09 -0430
From:      Andres Perera <andres.p@zoho.com>
To:        Maxim Khitrov <max@mxcrypt.com>
Cc:        Devin Teske <dteske@vicor.com>, FreeBSD <freebsd-questions@freebsd.org>
Subject:   Re: Shell script termination with exit function in backquotes
Message-ID:  <AANLkTimiciz9wy_fsYjdJXt2OTfAixaPD-=_mM59Dbyy@mail.gmail.com>
In-Reply-To: <AANLkTimWxiRQNG3Um__kY-6%2BQ59g5yZT-Kt0qLAqTWOO@mail.gmail.com>
References:  <AANLkTi=-CFmxRicGcosvzhBbM3DMjbWwQNirMrJ1_KP=@mail.gmail.com> <759A467E-407A-4DB8-9756-08011B5405F0@vicor.com> <AANLkTi=CXLFUBhnY1LuhkeUiGHHGZ43yd%2BMYE9L50_O4@mail.gmail.com> <AANLkTimrnV2rJLyc3M4e3gGy_GUDLXp128f6n8svM3_g@mail.gmail.com> <AANLkTim0GvnAyK3%2B=Bd1Sr=maz0B3Ybgve_c6FKWwfNs@mail.gmail.com> <AANLkTimWxiRQNG3Um__kY-6%2BQ59g5yZT-Kt0qLAqTWOO@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Mar 19, 2011 at 11:45 AM, Maxim Khitrov <max@mxcrypt.com> wrote:
>
> Here's another, but related, problem that I just ran into. The man page r=
eads:
>
> =C2=A0 =C2=A0 Commands may be grouped by writing either
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list)
> =C2=A0 =C2=A0 or
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 { list; }
> =C2=A0 =C2=A0 The first form executes the commands in a subshell. =C2=A0N=
ote that built-in
> =C2=A0 =C2=A0 commands thus executed do not affect the current shell...

and it also says that the rhs in a pipe is always executed in a subshell

>
> Here's my script:
>
> ----
> #!/bin/sh
>
> { A=3D1; }; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo $A
> echo | { B=3D2; }; =C2=A0 =C2=A0 =C2=A0echo $B
> { C=3D3; } > /dev/null; echo $C
> ----
>
> And here's the output:
>
> ----
> 1
>
> 3
> ----
>
> Where did the '2' go? Again, I have to assume that when stdin is piped
> to a group of commands, those commands are executed in a subshell
> despite curly braces. But where is this behavior documented? It seems
> that there are a lot of corner cases that can only be understood if
> you are familiar with the shell implementation. Documentation can
> certainly be improved in places.
>

this time it's a case of you not being familiar, and not violations of
principle of least suprise like ash command substitutions

the only broadly deployed shells that do not execute the (whole) rhs
in a subshell are ksh and descendants, and even then there are many
exceptions

the bracket grouping is irrelevant



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTimiciz9wy_fsYjdJXt2OTfAixaPD-=_mM59Dbyy>