Shell script termination with exit function in backquotes
Ian Smith
smithi at nimnet.asn.au
Sun Mar 20 15:43:50 UTC 2011
in freebsd-questions Digest, Vol 354, Issue 10, Message: 4
On Sat, 19 Mar 2011 12:15:26 -0400 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...
>
> 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.
See sh(1) /Pipelines - last para:
Note that unlike some other shells, sh executes each process in the pipe-
line as a child of the sh process. Shell built-in commands are the
exception to this rule. They are executed in the current shell, although
they do not affect its environment when used in pipelines.
The braces aren't relevant because it's a pipeline, so even without:
echo | B=2; echo $B
writes '', but
echo | { B=2; echo $B; }
or (equivalent within a pipeline)
echo | ( B=2; echo $B; )
writes '2'.
cheers, Ian
More information about the freebsd-questions
mailing list