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



Relevant Pages

  • Re: foreach and stack, iterating from the bottom-up?
    ... Andreas Leitgeb wrote: ... For loops are allowed with iterators. ... Foo foo = iter.next; ... And without scaring the people with falsehoods about 'for' loops, ...
    (comp.lang.java.programmer)
  • 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 ...
    (microsoft.public.sqlserver.programming)
  • 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; ...
    (comp.programming)
  • Re: Sorry for the NOOB question..
    ... do/while loops are more difficult to master ... more complex than "while (foo) {something)". ... cond is false), ...
    (comp.lang.c)
  • 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, ...
    (comp.unix.shell)