home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!ogicse!pnl-oracle!aggie!d3a061
- From: d3a061@aggie.oname (ZT Taylor)
- Newsgroups: comp.unix.shell
- Subject: SUMMARY: Bourne shell functions & 'exit'
- Message-ID: <1993Jan12.160725.19922@oracle.pnl.gov>
- Date: 12 Jan 93 16:07:25 GMT
- Article-I.D.: oracle.1993Jan12.160725.19922
- Sender: news@oracle.pnl.gov
- Reply-To: d3a061@aggie.oname
- Organization: Pacific Northwest Laboratory
- Lines: 49
-
-
- My original question:
-
- > Bourne shell functions seem to be inconsistent in the way they handle an 'exit'
- > statement. For example, the following:
- >
- > func() {
- > .
- > .
- > exit 1
- > }
- >
- > func
- >
- > causes the whole shell script to exit, while this:
- >
- > func() {
- > .
- > .
- > exit 1
- > }
- >
- > junk=`func`
- > echo $?
- >
- > doesn't. At first I thought this meant func was executed in a subshell, but
- > echoing $$ both places gives the same PID. Is this context-sensitive treatment
- > of 'exit' a feature? Can it be counted on in portable scripts?
-
- Thanks to Don Peterson <donp@hpbs1639.boi.hp.com> and Philip Guenther <guenther@stolaf.edu> for responding. Philip solved the mystery for me. It seems
- that Bourne shell functions executed in backticks ARE run in a subshell, but per
- POSIX standards, $$ in a subshell expands to the PID of the parent. To quote
- the POSIX standard (1003.2 section 3.5.2)...
-
- 3.5.2 Special Parameters
-
- Listed below are the special parameters and the values to which they
- shall expand. Only the values of the special parameters are listed; see
- 3.6 for a detailed summary of all the stages involved in expanding words.
- [...]
- $ Expands to the decimal process ID of the invoked shell. In a
- subshell (see 3.12), $ shall expand to the same value as that of
- the current shell.
-
- So...the behavior IS reliable and portable.
-
- Todd Taylor
- Pacific Northwest Laboratory
- zt_taylor@pnl.gov
-