Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 Aug 2002 12:20:12 +0200
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        Chris Knight <chris@aims.com.au>
Cc:        freebsd-stable@freebsd.org
Subject:   Re: variable scope issue in -stable?
Message-ID:  <20020811102010.GA40612@falcon.midgard.homeip.net>
In-Reply-To: <014d01c2411a$a2bee5d0$020aa8c0@aims.private>
References:  <014d01c2411a$a2bee5d0$020aa8c0@aims.private>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Aug 11, 2002 at 07:36:56PM +1000, Chris Knight wrote:
> Howdy,
> 
> I've got the following problem in FreeBSD-stable, and also 4.6, 4.5 and 4.2.
> The following code is not behaving as expected:
> -----
> #!/bin/sh
> match=0
> anycmd | while read line
> do
>   case "$line" in
>     "command")
>       match=1
>       echo "match=$match"
>       ;;
>    *)
>       ;;
>   esac
> done
> echo "match=$match"
> -----
> The output of the above results in:
> match=1
> match=0
> 
> I'd expect match to be a global definition, but match loses its value
> outside of the while block.
> It appears that read stuffs things up - if I change the while expression to
> something other than read, then the variable scope behaves correctly.
> Or am I missing something?

The following paragraph from the sh(1) manpage seems relevant:

     Note that unlike some other shells, sh executes each process in the
     pipeline as a child of the sh process.  Shell builtin commands are the
     exception to this rule.  They are executed in the current shell, although
     they do not affect its environment when used in pipelines.

So it is not the read, but the pipe that makes things behave differently
from what you expect.


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-stable" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020811102010.GA40612>