The expr Command

The expr command can be used to perform simple integer arithmetic. The general syntax of an expr command is

expr int1 op int2

Here, int1 and int2 are integers and op is one of the operators given in Table 18.3. The spaces separating op from int1 and int2 are required.

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

Let's look at a few examples that illustrate the use of expr. The first example illustrates multiplication:

$ expr 3 \* 5
15

Notice that the * sign must be escaped in order to prevent the shell from viewing it as a filename expansion meta-character. ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition 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.