Bug 71586 - read does not take input frmo a pipe
Summary: read does not take input frmo a pipe
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: bash
Version: 7.2
Hardware: i686
OS: Linux
medium
medium
Target Milestone: ---
Assignee: wdovlrrw
QA Contact: Ben Levenson
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2002-08-15 13:35 UTC by David Stevenson
Modified: 2007-04-18 16:45 UTC (History)
0 users

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2002-08-15 13:35:09 UTC
Embargoed:


Attachments (Terms of Use)

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


Note You need to log in before you can comment on or make changes to this bug.