delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/10/27/03:43:36

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 <jim AT cs DOT ualberta DOT ca>
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: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019