X-Spam-Check-By: sourceware.org Subject: Re: Piping to the 'read' command To: cygwin AT cygwin DOT com Date: Fri, 27 Oct 2006 01:43:08 -0600 (MDT) CC: Jim Easton X-Mailer: ELM [version 2.4ME+ PL66 (25)] Message-Id: <20061027074309.0FFFE5663C@erskine.cs.ualberta.ca> From: jim AT cs DOT ualberta DOT ca (Jim Easton) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Hi, Fri, 27 Oct 2006 Ian Taylor wrote: > Can anyone explain what is happening here (using pdksh as my shell) when > I try to set an environment variable using 'read': ... > > This doesn't work: > $ echo Test2 | read VAR2 > $ echo "VAR2 is $VAR2" > VAR2 is > > This works within the 'while' loop only: > $ echo Test3 | while read VAR3 > > do > > echo "VAR3 is $VAR3" > > done > VAR3 is Test3 > $ echo "VAR3 is $VAR3" > VAR3 is Hmm, I don't know but I did make a couple of observations: This check this out: $ echo Test2 | { read VAR2 > echo "VAR2 is $VAR2" > } VAR2 is Test2 $ echo second time VAR2 is $VAR2 second time VAR2 is $ I also noticed in your example line 2, (the one that reads; $ echo "VAR2 is $VAR2") you are being prompted with a "$" rather than a ">". Futhermore, VAR2 is unknown after the closing "}". I get the same result if the commands are in a script (See below) and on cygwin, solaris and linux. This suggests to me that it is executing that read in a subshell that can't pass the variable back to its parent. This dispite the fact that it appears to be the same process. (see inserted echo $$) I hope this is of some help. :-) Jim Easton #!/bin/bash echo $$ echo Test2 | { read VAR2 echo $$ echo "VAR2 is $VAR2" } echo second time VAR2 is $VAR2 -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/