[Novalug] bash weirdness

Ed James edjames at greenbelt.com
Fri Apr 9 16:30:48 EDT 2010


Chris,

    Try initializing the data[] variable outside/before the while
loop.

Ed James

Quoting "Chris Snyder" <gopher at 3wa.org>:

> Can somebody please explain to me what this bash snippet is doing?  Why is my
> array data disappearing outside of the 'while'?  Bash doesn't have scoping by
> default unless you use local or something like that.  WTF?!?
>
> thx
> Gopher.
>
> --------------------
> #!/bin/bash
>
> ls | while read output; do
>          data[$i]="$output"
>          let i++
>          echo "Current array size: " ${#data[*]}
> done
>
> echo "Final array size" ${#data[*]}
>
> --------------------
>
> Here's my output:
>
> bash y
> Current array size:  1
> Current array size:  2
> Current array size:  3
> Current array size:  4
> Current array size:  5
> Current array size:  6
> Current array size:  7
> Current array size:  8
> Current array size:  9
> Current array size:  10
> Current array size:  11
> Current array size:  12
> Current array size:  13
> Current array size:  14
> Current array size:  15
> Current array size:  16
> Current array size:  17
> Current array size:  18
> Final array size 0
>
>
> --
> gopher at 3wa.org
> "Evil is, as humans do" - The Misfits
> _______________________________________________
> Novalug mailing list
> Novalug at calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug
>





More information about the Novalug mailing list