Red Hat Bugzilla – Bug 71586
read does not take input frmo a pipe
Last modified: 2007-04-18 12:45:33 EDT
From Bugzilla Helper:
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0; Q312461)
Description of problem:
if I do 'echo hello | read line' nothing goes into $line in bash. It works
(and has always worked) in ksh. The man page suggests that it should work
because it is taking it frmo the standard input.
Version-Release number of selected component (if applicable):
Steps to Reproduce:
1.echo hello | read line
Actual Results: nothing ($line is empty)
Expected Results: 'hello' should be returned
This works in the korn shell on every type of unix I have tried. I would also
expect it to work in bash as it is better!
This is actually the correct behavior.
The part of the command after the pipe ("read", in this case) is executed in a
different process space, so the variable isn't available to the originally
You _can_ do:
whatever |while read a; do echo $a; done
(because the echo is in the same process space as the read)
You _can't_ do:
whatever |read a
(because the echo is in the same process space as "whatever", but not as
echo foo |read a && echo $a
Won't work, but
echo foo |(read a && echo $a)
Yes, fair enough. This is the difference between sh and ksh is it not, that
ksh spawns children differently, so echo hello | read line will work because it
passes it's environment down differently. I assume that bash behaves like sh
because it is a bourne again shell not a bourne again korn shell ;-)
My problem was that I was porting a shell script from a Sequent (ksh) to a
linux machine (bash) and found that the command I had been using for years
didn't work. I have just revamped the script a bit and all is well.
(Mind you - I've just noticed that it doesn't work in the RH7.2 ksh either - it
does in the PTX and SOlaris 5.8 ksh's)
Anyway, thanks for the explanation and your help.