The syntax resembles the shell's arithmetic facilities
that we have just seen. It is almost identical to the syntax of the C
`for` loop, except for the extra set of parentheses:

Here's the code; the explanation follows:

sum=0
count=$#
for ((i = 1; i <= count; i++))
do
let "sum += $1"
shift
done
print $sum

The first line initializes the variable `sum` to 0.
`sum` accumulates the sum of the arguments.
The second line is mostly for readability; `count`
indicates how many arguments there are. The third line starts
the loop itself. The variable `i` is the loop
control variable. The *init* clause sets it to
1, the *condition* clause tests it against the limit
`count`, and the *increment* clause
adds 1 to it each time around the loop. One thing you'll notice right
away is that inside the `for` loop header, there's no
need for the `$` in front of a variable name to get
that variable's value. This is true for any arithmetic expression in
the Korn shell.

The body of the loop does the addition.
It simply lets *let* do the math:
the addition is accomplished by adding `$1`
to the value in `sum`.
The *shift* command then
moves the next argument down into `$1` for use the next
time around the loop. Finally, when the loop is done, the script
prints the result.

The arithmetic `for` loop is particularly
handy for working with all the elements in an indexed array,
which we're about to see in the next section.

| | |

6.2. Numeric Variables and Arithmetic | | 6.4. Arrays |