7.6. Working with Numbers

While shell scripts seamlessly handle character strings, a little effort is needed to perform very basic arithmetic operations.

7.6.1. Binary operations

Adding or multiplying numbers together can be achieved using either expr or the $(( )) construct.

Example:

expr 7 + 3; expr 2 \* 10; expr 40 / 4; expr 30  11
$((7+3)); $((2*10)); $((40/4)); $((30-11))

7.6.2. Comparing values

Table 7.3. Test operators:

NumbersStrings
-lt< (sort strings lexicographically)
-gt> (sort strings lexicographically)
-len/a
-gen/a
-eq==
-ne!=