Unix scripts: how to sum numbers up

If you're ever thought of summing up more than two numbers in shell script, perhaps this basic post will be a good start for your Unix scripting experiments.

Basic construction for summing up in shell scripts

In my Basic arithmetic operations in Unix shell post last year, I've shown you how to sum up two numbers:

SUM=$(($NUMBER1 + $NUMBER2)

using the same approach, it's possible to calculate sums of as many numbers as you like, if you use one of the loops available in your shell.

Before we get started, let's create a simple file with numbers we'll work with:

ubuntu$ for i in 1 2 3 4 5 6 7 8 9; do echo $i >> /tmp/nums; done;
ubuntu$ cat /tmp/nums
1
2
3
4
5
6
7
8
9

Using a while loop to sum numbers up in Unix

Here's an example of how you can use a while loop in Unix shell for summing numbers up. Save it as a /tmp/sum.sh script, and don't forget to do chmod a+x /tmp/sum.sh so that you can run it!

#!/bin/sh
#
SUM=0
#
while read NUM
do
        echo "SUM: $SUM";
        SUM=$(($SUM + $NUM));
        echo "+ $NUM: $SUM";
done < /tmp/nums

Here's how the output will look when you run it:

ubuntu$ /tmp/sum.sh
SUM: 0
+ 1: 1
SUM: 1
+ 2: 3
SUM: 3
+ 3: 6
SUM: 6
+ 4: 10
SUM: 10
+ 5: 15
SUM: 15
+ 6: 21
SUM: 21
+ 7: 28
SUM: 28
+ 8: 36
SUM: 36
+ 9: 45

Of course, your data will most probably will be in a less readable form, so you'll have to do a bit of parsing before you get to sum the numbers up, but the loop will be organized in the same way.

That's it for today, enjoy and feel free to ask questions!

See also:

Related posts:


These posts may have more info on unix script add numbers:

  - No related posts.


7 comments ↓

#1 Ankan on 06.15.09 at 2:35 pm

Hello,

Can you please provide the following codes:

1. I divide 20 /6 . The answer id now 3. It sould be 3.33
2. How to do 10.222 + 1.1 = 11.322

Thanks in advance. :-)

#2 Floating point calculations in Unix scripts — UNIX Tutorial: Learn UNIX on 06.18.09 at 1:12 pm

[...] Summing numbers up in Unix scripts [...]

#3 Pete on 08.05.09 at 8:13 pm

Hello Ankan,
you need to use "scale" option from bc:
echo 'scale=25;57/43' | bc

Scale controls the number of decimal points. Not sure if scale is an option, command or what not… but that's your answer.

Regards,
Pete

#4 jeff on 03.24.10 at 6:20 pm

You can make your code tighter in ksh and bash:

SUM+=$NUM

instead of

SUM=$(($SUM + $NUM))

Also, you can use an awk one-liner instead of a while loop:

for i in 1 2 3 4 5; do echo $i; done | awk '{s+=$1} END {print s}'

or

cat | awk '{s+=$1} END {print s}'
1
2
3
4
5
^d
15 (result)

#5 Neeraj Batra on 11.08.10 at 8:42 am

Hi Pete

Thanks for your response, But the problem is that in below example

echo 'scale=25;57/43′ | bc

Values 57 and 43 are in variables in my script

i.e.

a=57
b=43
echo 'scale=25;$a/$b′ | bc

Above does not work :(

Plz help

Rgds
Neeraj Batra

#6 jeff on 12.12.10 at 6:49 pm

Neeraj Batra, you would need double quotes instead of single quotes to allow the shell to interpret the variables.

#7 Emil on 12.21.10 at 7:10 pm

Hello

I'm trying to sum a field in a | delimited file but not receiving the correct value. Not sure how I'm getting e+06. Please help
cat file.TXT |awk 'FS = "|" { sum += $12 } END { print sum }'|xargs -n1|while read sum ;do aa='$'$sum ;done
echo $aa
7.60411e+06

Thank you

Leave a Comment