shell script puzzler

Steve Harris vsh at etnibsd.UUCP
Tue Mar 21 03:34:09 AEST 1989


Here is one for the shell programming gurus:  given the following
shell script, how can I get the subshell to echo its true pid??
(I am running SunOS 3.4, if it matters).

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#! /bin/sh
echo $$			# echos pid of parent shell
( echo $$ )		# echos same pid
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I assume that when the shell encounters the ( list ) construction it
simply forks a copy of itself, with input limited to the list, perhaps
with I/O redirected, but with all variables preserved.  In particular,
the variable $$ seems to be preserved.  Is this assumption correct?

Using this construction (i.e., not replacing it with a here-document,
etc.), is there any SIMPLE way to get the pid of the subshell?

PS -- this puzzler is just for your entertainment, I have plenty of
work-arounds.
-- 
Steve Harris -- Eaton Corp. -- Beverly, MA -- uunet!etnibsd!vsh



More information about the Comp.unix.wizards mailing list