Attempting to understand behavior of /bin/ksh's read command

Dave Ihnat ignatz at dminet.com
Thu Nov 3 16:46:45 UTC 2005


On Thu, Nov 03, 2005 at 11:07:59AM -0500, Virden, Larry W. wrote:
> $ echo aa bb cc dd | read a b c d
> 
> $ echo $a
> 
> /bin/ksh: a: parameter not set

Nothing's wrong; it's doing just what you told it to do.  You just told
it to do the wrong thing.

You told it to spawn a subshell--note the pipe--which certainly read
the output from the echo command, set its own variables a, b, c, and
d--and then destroyed them when it exited.  You can't set variables in
a parent from a child.

If you type:

	read a b c d
	This is a test
	echo $a $b $c $d

you'll see it's parsed them properly; everything is in the same shell.

So how do you redirect input ?  There are various ways.

	exec 0<filename

	echo aa bb cc dd | (
		read a b c d
		echo $a $b $c $d
	)

and so on.

Cheers,
--
	Dave Ihnat
	ignatz at dminet.com




More information about the redhat-list mailing list