what's the use of "{ list }" in /bin/sh?

Jon H. LaBadie jon at jonlab.UUCP
Sat Jul 9 15:53:29 AEST 1988


In article <11755 at agate.BERKELEY.EDU>, ked at garnet.berkeley.edu (Earl H. Kinmonth) writes:
> >{
> >  echo "This is an example of how"
> >  echo "one can use the { list } "
> >  echo "construct to re-direct the"
> >  echo "output of several commands."
> >  date
> >  who
> >} >$HOME/test.log
> >
> 
> It should be noted that for this purpose { } has the advantage of
> NOT forking another shell unlike ( ) used in the same manner.

This would be true except for the use of redirection.  The example
will fork a child shell, effectively making { } and ( ) the same.
Note, this is not true of the Korn shell.

An example to demonatrate the effect.  

	x="The quick brown fox"
	echo "Before block 1:     $x"		# not redirected

	{
		echo "Starting block 1:   $x"
		x="jumped over the lazy dogs back"
		echo "Leaving block 1:    $x"
	}

	echo "After block 1:      $x"
	echo
	x="The quick brown fox"
	echo "Before block 2:     $x"		# redirected

	{
		echo "Starting block 2:   $x"
		x="jumped over the lazy dogs back"
		echo "Leaving block 2:    $x"
	} > /dev/tty

	echo "After block 2:      $x"

If a procedure stays in a single shell, then variables changed in the
blocked commands will also be changed after the block.  If on the other
hand, the block is executed in a child process (after a fork), neither
changed variables (nor changed directories) will be reflected by the
parents environment.

Using the Bourne shell, the output of this script is:

	Before block 1:     The quick brown fox
	Starting block 1:   The quick brown fox
	Leaving block 1:    jumped over the lazy dogs back
	After block 1:      jumped over the lazy dogs back

	Before block 2:     The quick brown fox
	Starting block 1:   The quick brown fox
	Leaving block 1:    jumped over the lazy dogs back
	After block 2:      The quick brown fox

-- 
Jon LaBadie
{att, ulysses, princeton}!jonlab!jon



More information about the Comp.unix.questions mailing list