Huh? That doesn't sound like any shell I've used over the past 25 years, including bash. The only thing I find in the manpage about variable scoping is using the local declaration inside functions.

If I change the while loop to be a for loop:

        for f in first second ;do

I get the expected results, namely that the final value is 'second', not 'initial'.

It also works if I take out the read, e.g.,:

        cat <<-EOF >/tmp/ttt
                first line
                second line
                EOF
        val=initial
        typeset -i line=1
        while true ;do
            val=$(sed -n "${line}p" /tmp/ttt | awk '{print $1}')
            echo "$line val is '$val'"
            test -n "$val"  ||  break
            ((line += 1))
        done
        echo "final val is '$val'


I'm not an expert in bash, and you obviously are, so quite possibly I'm missing something. Could you please show me the part of the manpage that I need to read to obtain enlightenment?


P.S.  My test program is attached.

NHA
---
Norman H. Azadian    Taegerishalde 13    CH-3110 Muensingen    Switzerland
[EMAIL PROTECTED]      tel: +41 31 721 7855      fax: +41 31 55 898 55


Debian Bug Tracking System wrote:
This is an automatic notification regarding your Bug report
#301433: bash: variables revert to original values when exiting loop involving 
read,
which was filed against the bash package.

It has been closed by one of the developers, namely
Matthias Klose <[EMAIL PROTECTED]>.

Their explanation is attached below.  If this explanation is
unsatisfactory and you have not received a better one in a separate
message then please contact the developer, by replying to this email.

Debian bug tracking system administrator
(administrator, Debian Bugs database)

Received: (at 301433-done) by bugs.debian.org; 25 Mar 2005 22:29:22 +0000
From [EMAIL PROTECTED] Fri Mar 25 14:29:21 2005
Return-path: <[EMAIL PROTECTED]>
Received: from mail.cs.tu-berlin.de [130.149.17.13] (root)
by spohr.debian.org with esmtp (Exim 3.35 1 (Debian))
id 1DExIv-00063f-00; Fri, 25 Mar 2005 14:29:21 -0800
Received: from mailhost.cs.tu-berlin.de ([EMAIL PROTECTED] [130.149.17.13])
by mail.cs.tu-berlin.de (8.9.3p2/8.9.3) with ESMTP id XAA10039;
Fri, 25 Mar 2005 23:29:17 +0100 (MET)
Received: from localhost (localhost [127.0.0.1])
by mailhost.cs.tu-berlin.de (Postfix) with ESMTP id CBD9BF217;
Fri, 25 Mar 2005 23:29:16 +0100 (MET)
Received: from mailhost.cs.tu-berlin.de ([127.0.0.1])
by localhost (bueno [127.0.0.1]) (amavisd-new, port 10224) with ESMTP
id 20858-30; Fri, 25 Mar 2005 23:29:15 +0100 (MET) 11244
Received: from bolero.cs.tu-berlin.de (bolero.cs.tu-berlin.de [130.149.19.1])
by mailhost.cs.tu-berlin.de (Postfix) with ESMTP;
Fri, 25 Mar 2005 23:29:15 +0100 (MET)
Received: (from [EMAIL PROTECTED])
by bolero.cs.tu-berlin.de (8.12.10+Sun/8.12.8/Submit) id j2PMTEDi023923;
Fri, 25 Mar 2005 23:29:14 +0100 (MET)
From: Matthias Klose <[EMAIL PROTECTED]>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <[EMAIL PROTECTED]>
Date: Fri, 25 Mar 2005 23:29:14 +0100
To: "Norman H. Azadian" <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
Subject: Re: Bug#301433: bash: variables revert to original values when exiting loop involving read
In-Reply-To: <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
X-Mailer: VM 7.03 under 21.4 (patch 6) "Common Lisp" XEmacs Lucid
X-Virus-Scanned: by amavisd-new at cs.tu-berlin.de
Delivered-To: [EMAIL PROTECTED]
X-Spam-Checker-Version: SpamAssassin 2.60-bugs.debian.org_2005_01_02 (1.212-2003-09-23-exp) on spohr.debian.org
X-Spam-Status: No, hits=-6.0 required=4.0 tests=BAYES_00,HAS_BUG_NUMBER autolearn=no version=2.60-bugs.debian.org_2005_01_02
X-Spam-Level:


Norman H. Azadian writes:

Package: bash
Version: 2.05b-24
Severity: important

The following script illustrates the problem:

        #!/bin/sh

        val=initial
        cat <<-EOF | while read ;do
                first line
                second line
                EOF
            val=$REPLY
            echo "val is '$val'"
        done
        echo "final val is '$val'"

When I run this, I get:

        val is 'first line'
        val is 'second line'
        final val is 'initial'

I suspect this only happens with read, but I'm not sure.


not a bug, the variable is set in another process (while ... done)
#!/bin/bash

echo === Test 1
val=initial
cat <<-EOF | while read ;do
        first line
        second line
        EOF
    val=loop
    echo "val is '$val'"
done
echo "final val is '$val'"


echo === Test 2
val=initial
cat <<-EOF | while read ;do
        first line
        second line
        EOF
    val=$REPLY
    echo "val is '$val'"
done
echo "final val is '$val'"


echo === Test 3
for f in first second ;do
    val=$f
    echo "val is '$val'"
done
echo "final val is '$val'"


echo === Test 4
cat <<-EOF >/tmp/ttt
        first line
        second line
        EOF
val=initial
typeset -i line=1
while true ;do
    val=$(sed -n "${line}p" /tmp/ttt | awk '{print $1}')
    echo "$line val is '$val'"
    test -n "$val"  ||  break
    ((line += 1))
done
echo "final val is '$val'"

Reply via email to