Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Mar 2011 18:07:13 -0700
From:      Devin Teske <dteske@vicor.com>
To:        RW <rwmaillists@googlemail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Shell script termination with exit function in backquotes
Message-ID:  <1300842433.5368.2.camel@dt.vicor.com>
In-Reply-To: <20110322123243.1257e695@gumby.homeunix.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> <EFA32C5B-1892-41C3-B34B-F96E75CA72CA@vicor.com> <20110322123243.1257e695@gumby.homeunix.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 2011-03-22 at 12:32 +0000, RW wrote:

> On Sat, 19 Mar 2011 09:44:57 -0700
> Devin Teske <dteske@vicor.com> wrote:
> 
> 
> > At least two variations to the rule that { ... } is a block of
> > commands executed in the current shell are:
> > 
> > 1. When the block appears as a function 
> 
> Is that correct?


It depends on where and how you are using the function.

For example, ...

    #!/bin/sh
    : ============
    abc()
    {
            x=2
    }
    : ============
    x=1
    abc
    echo $x
    # produces "2"
    : ============
    x=1
    : `abc`
    echo $x
    # produces "1"
    : ============
    x=1
    : `x=2`
    echo $x
    # produces "2"
    : ============
    x=1
    : `{ x=2; }`
    echo $x
    # produces "2"
    : ============

... in the above example, the function embedded within back-ticks is
executed in a sub-shell (but that is not to imply that back-ticks
themselves invoke a sub-shell -- as shown above, I put a simple
statement and a compound statement [surrounded in curlies] in back-ticks
and neither are executed in a sub-shell... only the function-call is
executed within a sub-shell when executed within back-ticks).



>  I'd assumed that functions do execute in the current
> shell since you can alter variables from a function


Functions may or may-not execute in a sub-shell depending on the context
in which they are used.


> , whilst you can't
> from a "()".


Correct, parenthesetical blocks always form a sub-shell.


> 
> e.g. 
> --------------------------------------------------------------------
> $ cat /tmp/foo
> #!/bin/sh
> 
> f (){
>   x=2
> }
> 
> x=1
> f
> echo $x
> ( x=3 )
> echo $x
> 
> --------------------------------------------------------------------
> 
> $ /tmp/foo
> 2
> 2
> 
> 
> 
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"


-- 
Cheers,
Devin Teske

-> FUN STUFF <-
-----BEGIN GEEK CODE BLOCK-----
Version 3.12
GAT/CS/B/CC/E/IT/MC/M/MU/P/S/TW d+(++) s: a- C+++@$ UB++++$ P++++@$ L
++++$ E-
W+++ N? o? K? w@ O M++$ V- PS+>++ PE@ Y+ PGP-> t(+) 5? X(+) R(-) tv+ b
+>++ DI+
D+(++) G++ e>++++ h r+++ z+++
------END GEEK CODE BLOCK------
Learn about the "Geek Code": http://www.geekcode.com/

-> LEGAL DISCLAIMER <-
This message  contains confidential  and proprietary  information
of the sender,  and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person  is strictly prohibited.  If you have  received this
message in error,  please notify  the e-mail sender  immediately,
and delete the original message without making a copy.

-> END TRANSMISSION <-



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