From owner-freebsd-questions Thu Jan 20 18:49:54 2000 Delivered-To: freebsd-questions@freebsd.org Received: from mailgw01.execpc.com (mailgw01.execpc.com [169.207.2.78]) by hub.freebsd.org (Postfix) with ESMTP id 55E3E153F0 for ; Thu, 20 Jan 2000 18:49:50 -0800 (PST) (envelope-from hamilton@pobox.com) Received: from woodstock.monkey.net (ryloon-2-88.mdm.mkt.execpc.com [169.207.76.216]) by mailgw01.execpc.com (8.9.1) id UAA26252; Thu, 20 Jan 2000 20:49:21 -0600 Received: from pobox.com (localhost [127.0.0.1]) by woodstock.monkey.net (Postfix) with ESMTP id F3AED72; Thu, 20 Jan 2000 20:52:18 -0600 (CST) X-Mailer: exmh version 2.1.1 10/16/1999 To: cjclark@home.com Cc: freebsd-questions@FreeBSD.ORG Subject: Re: sh(1) Messing with My Mind In-reply-to: Your message of "Thu, 20 Jan 2000 17:55:18 EST." <20000120175518.F72914@cc942873-a.ewndsr1.nj.home.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Thu, 20 Jan 2000 20:52:18 -0600 From: Jon Hamilton Message-Id: <20000121025219.F3AED72@woodstock.monkey.net> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In message <20000120175518.F72914@cc942873-a.ewndsr1.nj.home.com>, "Crist J. Cl ark" wrote: } On Thu, Jan 20, 2000 at 10:40:08PM +0100, Oliver Fromme wrote: } > Crist J. Clark wrote in list.freebsd-q } uestions: } > > [...] } > > seems to have muddled my thoughts this morning. Why does this happen: } > > } > > $ echo 3 | read NUM } > > $ echo $NUM } > > } > } > Because the read command is executed in a subshell when it is } > in a pipe. When the pipe ends, the subshell terminates, and } > its environment variables are gone. } } *grumble-grumble* } } I could swear I have done this in the past. [ use backticks instead ... ] } That is what I ususally do if the command generating the output is } short. There are two reasons I do not want to do this, } } (1) The command generating the output is long. (An awk command-line } program being fed by another pipe.) } } (2) A read would break up the output just the way I want. (The output } happens to be a number followed by a date(1)-type string. A } 'read NUM DATE' command would break it up exactly how I want.) If you only need those values for a while (something that you can easily loop through), you may want to try this: foo | bar | bletch | while read NUM DATE ; do # whatever # more whatever done and the variables will be in scope for the duration of the while loop. } And although it does not impact me, there is another reason, and one } of the best ones, that someone would rather pipe to read than } backtick, } } (3) The backticked argument cannot have nested backticks. That's not true, although it can get ugly when you have to start escaping backticks. The alternative is to use the $(command) syntax instead of `command` syntax. -- Jon Hamilton hamilton@pobox.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message