[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Perhaps variables are re-initialized after a while loop
From: |
Chet Ramey |
Subject: |
Re: Perhaps variables are re-initialized after a while loop |
Date: |
Wed, 27 Dec 2000 11:54:54 -0500 |
> Machine Type: i386-suse-linux
>
> Bash Version: 2.03
> Patch Level: 0
> Release Status: release
>
> Description:
> In the example below I have initialized a variable LINENUMBER
> with the value zero. Then I enter a loop to process a number
> of lines. In the loop the variable LINENUMBER is output.
> After the end of the loop the variable is reset to the initial
> value.
I don't see how you can expect any result other than what you got, since
you ran the while loop in a subshell. A subshell cannot affect its
parent's environment.
Read the Bash FAQ, question E4.
> The script (named x) I use to demonstrate the problem :
>
> #!/bin/bash
> let LINENUMBER=0
> egrep -v "^#|^ *$" $1 | \
> (while read LINE REST
> do
> let LINENUMBER=$LINENUMBER+1
> echo "$LINENUMBER $LINE $REST"
> done )
> echo "Total numbers of line : $LINENUMBER"
> exit 0
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)
Chet Ramey, CWRU chet@po.CWRU.Edu http://cnswww.cns.cwru.edu/~chet/