From owner-freebsd-hackers Tue Jul 13 9:13:58 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id F34C514D6F for ; Tue, 13 Jul 1999 09:13:47 -0700 (PDT) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.8.8/8.8.8) id SAA09770 for freebsd-hackers@FreeBSD.ORG; Tue, 13 Jul 1999 18:13:42 +0200 (CEST) (envelope-from olli) Date: Tue, 13 Jul 1999 18:13:42 +0200 (CEST) From: Oliver Fromme Message-Id: <199907131613.SAA09770@dorifer.heim3.tu-clausthal.de> To: freebsd-hackers@FreeBSD.ORG Subject: Re: bin/12578: `` subshell taints PWD Organization: Administration Heim 3 Reply-To: freebsd-hackers@FreeBSD.ORG MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: TIN [version 1.2 RZTUC(3) PL2] Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Niall Smart wrote in list.freebsd-hackers: > As I understand it most builtins will not spawn a new shell > when they are used in command substitution: > > niall% echo `echo $$` $$ > 20354 20354 > niall% Actually, that example doesn't prove anything. :-) $ echo `echo $$` $$ 8376 8376 $ echo `/bin/echo $$` $$ 8376 8376 $ echo `(/bin/echo $$)` $$ 8376 8376 $ echo `eval /bin/echo '$$'` $$ 8376 8376 $ echo `(eval /bin/echo '$$')` $$ 8376 8376 $ At first I thought that behaviour would be a bug, but it isn't. sh(1) says: $ Expands to the process ID of the invoked shell. A subshell retains the same value of $ as its parent. I.e. $$ does not change when a subshell is spawned, but it rather inherits the value of $$ from its parent. This makes sense, because it enables you to use constructs like this in shell scripts: touch tmp.$$ chmod 600 tmp.$$ ls | grep foo | while read file; do something ... >> tmp.$$ # <-- subshell! done do_more_things < tmp.$$ Command substitution certainly has to spawn a subshell, even for built-in commands, because otherwise you could modify parent shell variables within command substitutions. You can easily verify this with ktrace. ;-) Regards Oliver -- Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany (Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de) "In jedem Stück Kohle wartet ein Diamant auf seine Geburt" (Terry Pratchett) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message