Re: Shell script termination with exit function in backquotes



On Mon, Mar 14, 2011 at 6:40 PM, Andres Perera <andres.p@xxxxxxxx> wrote:
On Mon, Mar 14, 2011 at 7:46 AM, Maxim Khitrov <max@xxxxxxxxxxx> wrote:
On Mon, Mar 14, 2011 at 3:16 AM, Andres Perera <andres.p@xxxxxxxx> wrote:
On Sun, Mar 13, 2011 at 9:49 PM, Devin Teske <dteske@xxxxxxxxx> wrote:
If you make the changes that I've suggested, you'll have consistent execution. The reason you're having inconsistent behavior is because Linux has /bin/sh symbolically linked to /bin/bash while FreeBSD has a more traditional shell (we'll call it bourne shell "plus").

that is misleading because command substitutions have traditionally
invoked subshells, and freebsd sh(1)/ash is an exception, not the norm

in this case, ksh and bash deviates are clearly closer to standard
bourne behaviour


Thanks for that explanation. I can understand the benefits of
optimizing away subshell execution, but that can clearly lead to
unexpected behavior. Is there some documentation on when this
optimization is utilized (i.e. the command executed without a
subshell)? Would I be correct in assuming that it is only restricted
to built-in commands that are known not to produce any output, such as
'exit'?

i would check the source, autoconf docs, and http://www.in-ulm.de/~mascheck/

netbsd has  been patched to fix `exit 1`, according to the last site

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.

- Max
_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Shell script termination with exit function in backquotes
    ... optimizing away subshell execution, but that can clearly lead to ... Commands may be grouped by writing either ... The shell needs to create a new set of stdin/stdout file-descriptors for the block of commands that you've created, and executing said commands within a sub-shell achieves that. ...
    (freebsd-questions)
  • Re: Shell script termination with exit function in backquotes
    ... optimizing away subshell execution, but that can clearly lead to ... to built-in commands that are known not to produce any output, ...     Commands may be grouped by writing either ...
    (freebsd-questions)
  • Re: Bug in bash? different from ksh, at any rate...
    ... commands in pipelines. ... behavior exhibited by ksh. ... it's the pipeline that causes creation of the subshell. ...
    (Debian-User)
  • Re: interesting race condition problem with processes running in background
    ... # connecting to a machine, executing the commands, collect output ... complexity, but more importantly handles other cases as well. ... Seems that would be the much cleaner/simpler solution. ... How do you call subshell, ...
    (comp.unix.shell)
  • Re: Finding installed package files
    ... is a package with the common man pages for the thousand odd common commands ... Seeing as how 18 percent of the package is documentation - have you looked ... >a set of commands and 'man' pages, could produce such a directory to make it ... There are 16 books in front of me between the monitors, ...
    (alt.os.linux.redhat)