Strange shellscript behaviour?

From: Bjørn Augestad (boa_at_metasystems.no)
Date: 12/01/04


Date: Wed, 01 Dec 2004 19:32:46 GMT

Below is a tiny example of a problem I ran into today. The script
contains two loops. Both loops reads lines from a file and modifies FOO.
  For some strange reason FOO is reset to 1 after the first loop, but
keeps the value assigned(4) after the second loop.

Is this a bug or am I missing something obvious?

TIA
Bjørn

PS: This is GNU bash, version 2.05b.0(1)-release

#!/bin/bash
FOO=1
cat ~/.bashrc | while read line; do
         FOO=2
done
echo $FOO # Here foo is 1

FOO=3
while read line; do
         FOO=4
done < ~/.bashrc
echo $FOO # But here it is 4