Bourne to read?

Paul Falstad pfalstad at phoenix.Princeton.EDU
Fri Mar 1 16:41:18 AEST 1991


mosemann at sardion.unl.edu (Russell Mosemann) wrote:
>   In the Bourne shell, why does this not work
>      echo words | read W
>   but this does (if I put the words in the file)
>      read W < file

This is in the FAQ, although somewhat buried.  Basically, the pipe
forces read into a subshell in the first case.  sh spawns a subshell to
simplify file descriptor handling for the pipe; the subshell then reads
its input into the variable W, and then exits.  The parent shell's W is
not changed.  In the second (simpler) case, read is executed in the main
shell.  In some versions of sh (evidently not in SunOS), as well as in
ksh (I think) and in another shell whose name escapes me at the moment,
"echo words | read W" will return the expected result.

I don't know of any nice way around this.  If you can, redirect the
output to a file, and then do "read W <file".  If not, you could do
something gross like create a named pipe, etc.

--
Paul Falstad, pfalstad at phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
How DO you delete a file called "-"?  For viewers at home, the answer is
coming up on your screen.  For those of you who wish to play it the hard
way, stand upside down with your head in a bucket of piranha fish.



More information about the Comp.unix.questions mailing list