Skip to main content
Tweeted twitter.com/#!/StackUnix/status/573680624424390656
added 82 characters in body
Source Link
iruvar
  • 17k
  • 8
  • 51
  • 81

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

The length of an array in my bash shell script appears to be off by one. I have 11 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 < students.dat

echo $count

ArrayLength=$((${#students[@]}))

echo $ArrayLength

This code outputs:

11 12

The 11 makes sense since the count increment occurs after the read occurs and starts with 0--indicating 11 elements read in.

But the 12 is mysterious.

Here is the data file:

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

The length of an array in my bash shell script appears to be off by one. I have 11 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 < students.dat

echo $count

ArrayLength=$((${#students[@]}))

echo $ArrayLength

This code outputs:

11
12

The 11 makes sense since the count increment occurs after the read occurs and starts with 0--indicating 11 elements read in.

But the 12 is mysterious.

Here is the data file:

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.

Source Link
Joe
  • 158
  • 6

Bash Shell Script Array Length Off By One

The length of an array in my bash shell script appears to be off by one. I have 11 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 < students.dat

echo $count

ArrayLength=$((${#students[@]}))

echo $ArrayLength

This code outputs:

11 12

The 11 makes sense since the count increment occurs after the read occurs and starts with 0--indicating 11 elements read in.

But the 12 is mysterious.

Here is the data file:

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