This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: Piping to the 'read' command
- From: jim at cs dot ualberta dot ca (Jim Easton)
- To: cygwin at cygwin dot com
- Cc: Jim Easton <jim at cs dot ualberta dot ca>
- Date: Fri, 27 Oct 2006 01:43:08 -0600 (MDT)
- Subject: Re: Piping to the 'read' command
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/