How can I quickly sum all numbers in a file?

Solution 1:

You can use awk:

awk '{ sum += $1 } END { print sum }' file

Solution 2:

None of the solution thus far use paste. Here's one:

paste -sd+ filename | bc

As an example, calculate Σn where 1<=n<=100000:

$ seq 100000 | paste -sd+ | bc -l
5000050000

(For the curious, seq n would print a sequence of numbers from 1 to n given a positive number n.)