Arithmetic Expansion

Command substitution uses the expression $(...), where the output of what is inside the parentheses replaces the expression. Arithmetic expansion uses a similar expression: $(( ... )).

An easy example demonstrates this. The following is a simple program for performing arithmetic expansion:

#!/bin/ksh
a=5
b=6
c=$(( a*b ))
echo "$a * $b equals $c"

At first glance, the reader is probably unsure what will happen with the echo command. Here is the output of the script (named Example55.ksh):

$ ./Example55.ksh
5 * 6 equals 30

Notice that the echo command does not expand the math, but gives it literally. A common error here is in removing the double quotes from around the echo command. Here is the program without the double quotes: ...

Get Korn Shell Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.