Date: Tue, 13 Jul 1999 18:13:42 +0200 (CEST) From: Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de> To: freebsd-hackers@FreeBSD.ORG Subject: Re: bin/12578: `` subshell taints PWD Message-ID: <199907131613.SAA09770@dorifer.heim3.tu-clausthal.de>
next in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199907131613.SAA09770>
