From owner-freebsd-questions@FreeBSD.ORG Wed Mar 23 01:07:52 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CB074106566C for ; Wed, 23 Mar 2011 01:07:52 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id AE8998FC0A for ; Wed, 23 Mar 2011 01:07:52 +0000 (UTC) Received: from [192.82.228.150] (port=57149) by postoffice.vicor.com with esmtpsa (SSLv3:AES256-SHA:256) (Exim 4.74) (envelope-from ) id 1Q2CYE-0003g7-5h; Tue, 22 Mar 2011 18:07:56 -0700 From: Devin Teske To: RW In-Reply-To: <20110322123243.1257e695@gumby.homeunix.com> References: <759A467E-407A-4DB8-9756-08011B5405F0@vicor.com> <20110322123243.1257e695@gumby.homeunix.com> Organization: VICOR, Inc. Date: Tue, 22 Mar 2011 18:07:13 -0700 Message-ID: <1300842433.5368.2.camel@dt.vicor.com> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 X-Scan-Signature: 431a13a277cf820871f488ba670a8931 X-Scan-Host: postoffice.vicor.com Content-Type: text/plain; charset="cp1252" Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-questions@freebsd.org Subject: Re: Shell script termination with exit function in backquotes X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Mar 2011 01:07:52 -0000 On Tue, 2011-03-22 at 12:32 +0000, RW wrote: > On Sat, 19 Mar 2011 09:44:57 -0700 > Devin Teske 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 <-