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:[EMAIL PROTECTED])
"In jedem St�ck Kohle wartet ein Diamant auf seine Geburt"
(Terry Pratchett)
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message