I have a txt file with integers on each line and i want the sum.
SUM=0
while read line || [ -n "$line" ] ; do
let "SUM += line"
done
echo $SUM
The problem is that while most of the time I get the correct result, sometimes I get SUM+1.
How can this be explained?
Input file:
2
38
21
50
42
59
19
19
29
17
24
13
45
49
48
13
20
20
51
41
51
45
1
17
6
46
52
50
32
19
8
28
35
33
13
4
2
49
32
19
17
17
16
51
23
32
26
54
31
54
14
17
55
53
18
34
4
16
15
Expected sum: 1709, sometimes I get 1710.