From owner-freebsd-questions@FreeBSD.ORG Sun Mar 20 01:00:12 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 0C4F21065672 for ; Sun, 20 Mar 2011 01:00:12 +0000 (UTC) (envelope-from 839273@gmail.com) Received: from mail-yw0-f54.google.com (mail-yw0-f54.google.com [209.85.213.54]) by mx1.freebsd.org (Postfix) with ESMTP id 689BD8FC14 for ; Sun, 20 Mar 2011 01:00:10 +0000 (UTC) Received: by ywf9 with SMTP id 9so2315944ywf.13 for ; Sat, 19 Mar 2011 18:00:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:reply-to:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=CzTwSpm4uoiKUlRR4nqq9naP+BsUeGrZYwiVxPP1Aao=; b=q0DWyHkpG/GXQAuTAGlx5W/5gS6Rq2KtTbRYVYcj4KTESRw5fFXzo0ahpJumAwr+/U eEO+EcHm0q2fP+PWOBPro+a76VXN1iWiYEjvgEY2tEqbF3TY2Sy+0aQOKKyVgfksfnNP jlU+kFlBfhHLbXbv3H3WTqslsXjeMBXBEETIc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:reply-to:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=ZKlT6KbySVw8K+UBMCT5vVdxnCABgR0RLHFq8G88/cj04KxmarugMYZDR94v616Zbp ltuMhcyeGJFsG5VzRXxQV46MlNGblAVUkiBmMQ5R7rEcmv7uiI+R5cHKfmLwkIcfWyFE pItz3JB3WIvyaZKcfkjB07l1hwOS6Q45zmYNQ= MIME-Version: 1.0 Received: by 10.150.169.8 with SMTP id r8mr2508868ybe.447.1300582809351; Sat, 19 Mar 2011 18:00:09 -0700 (PDT) Sender: 839273@gmail.com Received: by 10.150.216.2 with HTTP; Sat, 19 Mar 2011 18:00:09 -0700 (PDT) In-Reply-To: References: <759A467E-407A-4DB8-9756-08011B5405F0@vicor.com> Date: Sat, 19 Mar 2011 20:30:09 -0430 X-Google-Sender-Auth: rISZAEAaRpUBah0fcyFWjUlolFk Message-ID: From: Andres Perera To: Maxim Khitrov Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Devin Teske , FreeBSD Subject: Re: Shell script termination with exit function in backquotes X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: 839273@gmail.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Mar 2011 01:00:12 -0000 On Sat, Mar 19, 2011 at 11:45 AM, Maxim Khitrov wrote: > > Here's another, but related, problem that I just ran into. The man page r= eads: > > =C2=A0 =C2=A0 Commands may be grouped by writing either > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list) > =C2=A0 =C2=A0 or > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 { list; } > =C2=A0 =C2=A0 The first form executes the commands in a subshell. =C2=A0N= ote that built-in > =C2=A0 =C2=A0 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=3D1; }; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo $A > echo | { B=3D2; }; =C2=A0 =C2=A0 =C2=A0echo $B > { C=3D3; } > /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