This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: 'read' and pipes, was: 1.5.9: Trouble with setting variables using 'read' in a script


> Another related quirk, is that variables set within
> 'while read' loops lose
> their values once the loop ends.  The following example
> displays "text text"
> within the loop and blank line outside.
>   - echo "text" |\
>     while read
>     do
>       foo=$REPLY ; bar="text"
>       echo $foo $bar
>     done
>     echo $foo $bar
>
> Is this a feature of bash or a bug?  If it is a feature is there a
> workaround?

As I learned shortly ago, the pipe '|' always forces
a new subshell. This holds for any shell, though someone
said that they differ in which side of the pipe gets
to be the subshell and which will be executed in the
main shell.

Thus, in your example, $foo and $bar only exist in the
subshell and will never be known to the surrounding
shell.

I think you can get closer to it using redirection



--
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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]