Strange shellscript behaviour?

From: Bjørn Augestad (
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?


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

cat ~/.bashrc | while read line; do
echo $FOO # Here foo is 1

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

Relevant Pages

  • Re: foreach and stack, iterating from the bottom-up?
    ... Andreas Leitgeb wrote: ... For loops are allowed with iterators. ... Foo foo =; ... And without scaring the people with falsehoods about 'for' loops, ...
  • Re: Maximum number of bits
    ... I posted contents of a lookup table with 17 rows to cover the first set of combinations. ... The "nested loops within loops" refers to a procedural looping solution. ... CREATE TABLE Foo; ... > management and physical I/O (unless you have 40 GB of RAM dedicated to ...
  • Re: strlen(), K+1: clarification
    ... In article, Richard Harter ... The new terminating condition is a comparison with 0, ... practice to preferentially write descent loops rather than ascent ... foo; ...
  • Re: Sorry for the NOOB question..
    ... do/while loops are more difficult to master ... more complex than "while (foo) {something)". ... cond is false), ...
  • Re: Bash bug?
    ... function foo() { ... echo Foo | while read x ... the right-hand-side of the pipe runs in a subshell. ... When the 2nd process finished, your commandline shell reaped it's status, ...