< Previous | Contents | Next >
Simple Arithmetic
The ordinary arithmetic operators are listed in the table below:
Table 34-3: Arithmetic Operators
Operator Description
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Integer division
** Exponentiation
% Modulo (remainder)
Most of these are self-explanatory, but integer division and modulo require further dis- cussion.
Since the shell’s arithmetic only operates on integers, the results of division are always whole numbers:
[me@linuxbox ~]$ echo $(( 5 / 2 ))
2
[me@linuxbox ~]$ echo $(( 5 / 2 ))
2
This makes the determination of a remainder in a division operation more important:
[me@linuxbox ~]$ echo $(( 5 % 2 ))
1
[me@linuxbox ~]$ echo $(( 5 % 2 ))
1
By using the division and modulo operators, we can determine that 5 divided by 2 results in 2, with a remainder of 1.
Calculating the remainder is useful in loops. It allows an operation to be performed at specified intervals during the loop's execution. In the example below, we display a line of numbers, highlighting each multiple of 5:
#!/bin/bash
# modulo: demonstrate the modulo operator for ((i = 0; i <= 20; i = i + 1)); do
remainder=$((i % 5))
if (( remainder == 0 )); then printf "<%d> " $i
else
printf "%d " $i
fi done
printf "\n"
#!/bin/bash
# modulo: demonstrate the modulo operator for ((i = 0; i <= 20; i = i + 1)); do
remainder=$((i % 5))
if (( remainder == 0 )); then printf "<%d> " $i
else
printf "%d " $i
fi done
printf "\n"
When executed, the results look like this:
[me@linuxbox ~]$ modulo
<0> 1 2 3 4 <5> 6 7 8 9 <10> 11 12 13 14 <15> 16 17 18 19 <20>
[me@linuxbox ~]$ modulo
<0> 1 2 3 4 <5> 6 7 8 9 <10> 11 12 13 14 <15> 16 17 18 19 <20>