next up previous contents
Next: Interactive programs in shell Up: Shell programming Previous: Loops and conditional statements   Contents

Arithmetic in shell scripts

Normally variables in shell scripts are treated as strings. To use numerical variables, enclose expressions in square brackets.

#!/bin/sh
sum=0
for x in `cat data`
do
        sum=$[sum+$x]
done
echo "sum=" $sum



Amit Jain 2013-04-22