Bug 71586

Summary: read does not take input frmo a pipe
Product: [Retired] Red Hat Linux Reporter: David Stevenson <d.j.stevenson>
Component: bashAssignee: wdovlrrw <brosenkr>
Status: CLOSED NOTABUG QA Contact: Ben Levenson <benl>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.2   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2002-08-15 13:35:09 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description David Stevenson 2002-08-15 13:35:05 UTC
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):


How reproducible:
Always

Steps to Reproduce:
1.unset line
1.echo hello | read line
2.echo $line
3.
	

Actual Results:  nothing ($line is empty)

Expected Results:  'hello' should be returned

Additional info:

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!

Comment 1 Bernhard Rosenkraenzer 2002-08-15 13:46:26 UTC
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 
calling shell. 
 
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 
echo $a 
 
(because the echo is in the same process space as "whatever", but not as 
"read"). 
 
Similarily: 
 
echo foo |read a && echo $a 
 
Won't work, but 
 
echo foo |(read a && echo $a) 
 
will. 


Comment 2 David Stevenson 2002-08-15 15:04:55 UTC
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.

David