public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* 1.5.9: Trouble with setting variables using 'read' in a script
@ 2004-05-06 13:25 Kevan Gelling
  2004-05-06 14:20 ` Andy Rushton
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Kevan Gelling @ 2004-05-06 13:25 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'


I'm having trouble setting variables using the 'read' command in bash.

All of the following lines fail to set $var and return a blank line.
  - echo "text" | read var ; echo $var
  - cat file | read var ; echo $var
  - read var < file | echo $var

I can get it work by explicitly declaring the file descriptor with the file
redirection, but I'd prefer to use a pipe.
  - read -u 0 var <file ; echo $var

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?

My setup is Cygwin 1.5.9-1 and Bash 2.05b-16 under Windows XP Pro 5.1 SP1

Thanks,
Kevan


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 1.5.9: Trouble with setting variables using 'read' in a script
  2004-05-06 13:25 1.5.9: Trouble with setting variables using 'read' in a script Kevan Gelling
@ 2004-05-06 14:20 ` Andy Rushton
  2004-05-06 14:22 ` 'read' and pipes, was: " Jan Schormann
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Andy Rushton @ 2004-05-06 14:20 UTC (permalink / raw)
  To: Kevan Gelling; +Cc: Cygwin mailing list

Kevan Gelling wrote:

>I'm having trouble setting variables using the 'read' command in bash.
>
>All of the following lines fail to set $var and return a blank line.
>  - echo "text" | read var ; echo $var
>  - cat file | read var ; echo $var
>  - read var < file | echo $var
>  
>
The problem is that the pipeline is executed by spawning a sub-process 
and running a sub-shell in it. The variable that is being read into is 
actually a copy that exists within that sub-shell, not the original that 
you declared in the script. Variables set in sub-shells cannot be passed 
back to their parent shell (the script itself).

>I can get it work by explicitly declaring the file descriptor with the file
>redirection, but I'd prefer to use a pipe.
>  - read -u 0 var <file ; echo $var
>  
>
This doesn't create a sub-shell, so the variable being read is the one 
in the 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
>  
>
The while loop also becomes a sub-shell. Same problem as the pipeline.

This is a 'feature' of bash and indeed all Bourne-shell derivatives. It 
makes a lot of shell features much harder to use than you would expect.

Incidentally, this is why you cannot write shell scripts to change your 
environment. The script runs in a sub-shell, changes the local copy of 
the environment and then discards the local copy on exit leaving the 
parent shell in the same state as it was in before.

Andy

-- 
Andy Rushton, Southampton, UK

We may eventually come to realize that chastity is no more a virtue
than malnutrition.
		-- Alex Comfort


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: 'read' and pipes, was: 1.5.9: Trouble with setting variables using 'read' in a script
  2004-05-06 13:25 1.5.9: Trouble with setting variables using 'read' in a script Kevan Gelling
  2004-05-06 14:20 ` Andy Rushton
@ 2004-05-06 14:22 ` Jan Schormann
  2004-05-06 15:27 ` cont'd - " Jan Schormann
  2004-05-06 16:36 ` Brian Dessent
  3 siblings, 0 replies; 5+ messages in thread
From: Jan Schormann @ 2004-05-06 14:22 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* cont'd - RE: 1.5.9: Trouble with setting variables using 'read' in a script
  2004-05-06 13:25 1.5.9: Trouble with setting variables using 'read' in a script Kevan Gelling
  2004-05-06 14:20 ` Andy Rushton
  2004-05-06 14:22 ` 'read' and pipes, was: " Jan Schormann
@ 2004-05-06 15:27 ` Jan Schormann
  2004-05-06 16:36 ` Brian Dessent
  3 siblings, 0 replies; 5+ messages in thread
From: Jan Schormann @ 2004-05-06 15:27 UTC (permalink / raw)
  To: cygwin

(Please excuse the unfinished last mail, I hit "Ctrl-Enter"
by accident.)

>   - echo "text" | read var ; echo $var
>   - cat file | read var ; echo $var
>   - read var < file | echo $var

.. continuing from last time, all of these won't work
because you're always creating a new subshell.

Try

  read var < file ; echo $var

Cheers,
	Jan.



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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: 1.5.9: Trouble with setting variables using 'read' in a script
  2004-05-06 13:25 1.5.9: Trouble with setting variables using 'read' in a script Kevan Gelling
                   ` (2 preceding siblings ...)
  2004-05-06 15:27 ` cont'd - " Jan Schormann
@ 2004-05-06 16:36 ` Brian Dessent
  3 siblings, 0 replies; 5+ messages in thread
From: Brian Dessent @ 2004-05-06 16:36 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'

Kevan Gelling wrote:

> All of the following lines fail to set $var and return a blank line.
>   - echo "text" | read var ; echo $var
>   - cat file | read var ; echo $var
>   - read var < file | echo $var

The other reply by Andy R is spot on, you can't modify the current
environment from a subshell.  If you want to use the above idioms, you
can use:

echo "text" | (read var; echo $var)

This causes the 'echo' to be run from the same subshell as 'read', so it
will see the modified environment.  None of this is specific to Cygwin,
it's the design of sh/ash/bash type shells, and so there are better
places to ask about these kinds of things than the Cygwin list.  Try
looking for newsgroups, FAQs, or books dealing with Bourne shell
programming.

Brian

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-05-06 16:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-06 13:25 1.5.9: Trouble with setting variables using 'read' in a script Kevan Gelling
2004-05-06 14:20 ` Andy Rushton
2004-05-06 14:22 ` 'read' and pipes, was: " Jan Schormann
2004-05-06 15:27 ` cont'd - " Jan Schormann
2004-05-06 16:36 ` Brian Dessent

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).