shell script puzzler

Daniel R. Levy levy at ttrdc.UUCP
Sun Mar 26 15:01:15 AEST 1989


[Chris Torek]
< >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
< 
< At least in 4BSD, you cannot.  `sh' sets up `$$' near the top of main()
< and never changes it (except when running a script, in lieu of starting
< a new /bin/sh to run it).

Yup that's basically what you have to do, start a new shell explicitly.

If you want to see that pid at the END of the subshell, just

(
...
exec echo '$$'
#nothing from here on will be executed
...
)

Since the exec retains the same pid, it works.  Of course anything from
there to the end of the subshell gets pitched... JUST a picky little
detail....
-- 
Daniel R. Levy             UNIX(R) mail:  att!ttbcad!levy
AT&T Bell Laboratories
5555 West Touhy Avenue     Any opinions expressed in the message above are
Skokie, Illinois  60077    mine, and not necessarily AT&T's.



More information about the Comp.unix.wizards mailing list