Arithmetic Integer Calculations

The expr command performs simple integer arithmetic:

$ expr 8 / 3
2
$

Notice that any fractional result is ignored. The general syntax is as follows:

expr integer1 operand integer2

Possible operands for this command are given in Table 6.1.

Table 6.1. expr Operands
OperandDescription
+Addition
-Subtraction
\*Multiplication
/Integer division (any fraction in the result is dropped)
%Remainder from a division operation (also called the modulus function)

Notice that the * sign must be enclosed in quotation marks to prevent shell expansion (see the section “Quoting,” in Chapter 4, “Rules for Entering UNIX Commands”), but the spaces around the * sign must not be quoted, as in the following example:

$ expr 3 \* 5
15
$

The remainder ...

Get Practical UNIX 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.