The length of an array in my bash shell script appears to be off by one. I have 1111 elements in a text file that I am reading in to an array, but my array length seems to be 12.
(( count = 0 ))
while read students[$count] ; do
(( count++ ))
done < students12.dat
echo $count
ArrayLength=$((${#students[@]}))
echo $ArrayLength
(( count = 0 ))
while read students[$count] ; do
(( count++ ))
done < students.dat
echo $count
ArrayLength=$((${#students[@]}))
echo $ArrayLength
This code outputs:
11 12
11
12
The 1111 makes sense since the count increment occurs after the read occurs and starts with 00--indicating 1111 elements read in.
But the 1212 is mysterious.
Here is the data file:
Ann Bob Cindy Dean Emily Frank Ginger Hal Ivy Justin Karen
Ann
Bob
Cindy
Dean
Emily
Frank
Ginger
Hal
Ivy
Justin
Karen
(names appear on their own lines, but I couldn't format it that way in this post)
I've double checked with multiple utilities and there is NOT a blank line at the end of the file, or any trailing spaces on any line.
Justin