Arithmetic in shell scripts

Normally variables in shell scripts are treated as strings. To use numerical variables, enclose expressions in square brackets. For example, here is a code snippet that adds up the integers from 1 to 100.

[alice@localhost ~]$ for ((i=0; i<=100; i++))
> do
>   sum=$[sum + i]
> done
[alice@localhost ~] echo $sum
5050

Here is a script that adds up the first column from a text data file:

#!/bin/sh
# addData.sh
sum=0
for x in $(cat data.txt | awk '{print $1}')
do
    sum=$[sum+$x]
done
echo "sum =" $sum

Here is a sample run:

[alice@localhost sandbox] cat data.txt 
100 3.5
200 3.5
300 3.5
400 3.5
500 3.5
600 3.5
[alice@localhost sandbox] ./addData.sh 
sum = 2100