Shell script termination with exit function in backquotes

Andres Perera andres.p at zoho.com
Sun Mar 20 01:00:12 UTC 2011


On Sat, Mar 19, 2011 at 11:45 AM, Maxim Khitrov <max at mxcrypt.com> wrote:
>
> Here's another, but related, problem that I just ran into. The man page reads:
>
>     Commands may be grouped by writing either
>           (list)
>     or
>           { list; }
>     The first form executes the commands in a subshell.  Note that built-in
>     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=1; };             echo $A
> echo | { B=2; };      echo $B
> { C=3; } > /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


More information about the freebsd-questions mailing list