What's wrong with this Bourne shell script?

Maarten Litmaath maart at cs.vu.nl
Tue Aug 7 02:15:49 AEST 1990


In article <3821 at auspex.auspex.com>,
	guy at auspex.auspex.com (Guy Harris) writes:
)...
)The Bourne shell runs some shell constructs in a sub-shell; apparently,
)the construct you gave is one of them.  [...]

[The construct was ``command | while read line; do something; done''.]

)This may change when shells become compliant with POSIX 1003.2, which
)will presumably happen sometime after POSIX 1003.2 leaves the draft
)stage and is approved; 1003.2 may put stricter controls on when a shell
)may do something inside a subshell.  I don't have recent drafts handy;
)Maarten?

I haven't read Draft 10 yet, but I pleaded for the `while' loop from the
example to be run in the current shell instead of a subshell (if the user
wants a subshell he can always use parentheses).  The advantages are
obvious.

In any case, _redirected_ loops already must be run in the current shell,
notwithstanding the current practice in a lot of Bourne shell implementations
to create a subshell:

	var=before
	while read line
	do
		var=after
	done < file
	echo var=$var

This example will print `after' on POSIX 1003.2 conformant systems,
`before' on many current systems.
--
   "UNIX was never designed to keep people from doing stupid things, because
    that policy would also keep them from doing clever things."  (Doug Gwyn)



More information about the Comp.unix.questions mailing list