why doesn't this shell program work

johnl at ima.UUCP johnl at ima.UUCP
Mon Mar 4 14:48:35 AEST 1985


Congratulations!  You have tripped over one of the most peculiar parts
of the Bourne shell.  The problem is this:  the shell only knows how to
do I/O redirection for processes, not for builtin commands.  This means
that if you redirect I/O for a command, it gets forked into a subprocess
silently and confusingly.

For example:

a=one

for i in two three four
do
	a=$i
done > plugh

echo $a

The result is "one" because the for loop is done in a subprocess, and the
modifications to variable a in the subprocess don't affect the top level
shell.  See Steve Bourne's book "The Unix System" for some extremely gross
ways to work around this.  (I suppose that since it's his shell, he gets to
program it any way he wants, though.)

Also, the System V shell is somewhat smarter about I/O redirection, so
some things like "read foo <bar" which used not to work now do.

John Levine, ima!johnl



More information about the Comp.unix mailing list