17.5. Using expr

The expr command is used mostly for integer values, but can be used for strings as well. Its general format is:

expr argument operator argument

Expr is a handy command line calculator as well:

						$ expr 10 + 10
20

$ expr 900 + 600
1500

$ expr 30 / 3
10

$ expr 30 / 3 /2
5

When using the multiply sign, you will have to escape it with a backslash, because the shell might misinterpret the asterisk ‘*’:

						$ expr 30 \* 3
90
					

17.5.1. Incrementing counters

Expr does the incrementing of values when using loops. First the loop is initialized to zero. Then one is added to the variable loop. The use of the quotes means command substitution, which basically means take the output from the command (expr) and put it in the variable loop.

							$ LOOP=0 ...

Get Linux and Unix Shell Programming 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.