This is the mail archive of the
mailing list for the Cygwin project.
RE: Piping to the 'read' command
- From: "Dave Korn" <dave dot korn at artimi dot com>
- To: <cygwin at cygwin dot com>
- Date: Fri, 27 Oct 2006 11:32:06 +0100
- Subject: RE: Piping to the 'read' command
On 27 October 2006 04:04, firstname.lastname@example.org wrote:
> Can anyone explain what is happening here (using pdksh as my shell) when
> I try to set an environment variable using 'read':
Variables set in a subshell process are not returned back into the parent
> This works:
> $ read VAR1
> $ echo "VAR1 is $VAR1"
> VAR1 is Test1
VAR1 exists in the main pdksh process. It is set by read and displayed by
> This doesn't work:
> $ echo Test2 | read VAR2
> $ echo "VAR2 is $VAR2"
> VAR2 is
The second process on the end of the pipe is a subshell. VAR2 is set in the
subshell. When the command terminates the subshell exits, VAR2 is lost, and
you are left at the command prompt in the parent shell, which is not where
VAR2 was set. To see the value of VAR2 in the subshell, combine the echo with
the read like so:
\u@\h \w> echo Test2 | ( read VAR2 ; echo "VAR2 is $VAR2" )
VAR2 is Test2
> This works within the 'while' loop only:
> $ echo Test3 | while read VAR3
>> echo "VAR3 is $VAR3"
> VAR3 is Test3
> $ echo "VAR3 is $VAR3"
> VAR3 is
The body of the loop is the subshell process. VAR3 again exists only within
the subshell where it is set.
Can't think of a witty .sigline today....
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html