Bash and arrays

Jay Hall jhall at socket.net
Wed Jul 15 01:57:25 UTC 2009


Ladies and Gentlemen,

I thought I understood how arrays work in bash, but I have been proven  
wrong.  I am reading lines from a file and placing them in an array.   
However, when I am finished, the array has a length of 0.

Following is the code I am using.

#!/usr/local/bin/bash
COUNTER=0
cat ./test_file.txt | while read LINE
do
         echo ${LINE}
         FOO[${COUNTER}]=${LINE}
         COUNTER=`expr ${COUNTER} + 1`
done

echo ${#FOO[@]}
echo ${#FOO[*]}


And, here is the output.

test_file
file_size
0
0

Thanks in advance for any help you can offer.


Jay


More information about the freebsd-questions mailing list