From NovaOrdis Knowledge Base
Jump to: navigation, search



"expr" evaluates an expression and prints the value of that expression at stdout. All operators and operands must be passed as separate arguments. Several of the operators have special meaning to command interpreters (like '*') and must therefore be quoted appropriately. * must be passed as \*.

For +, -, *, / and % the arguments must be integers, otherwise we'll get:

expr: not a decimal number: '10.1'

To do floating point arithmetic in bash, you'll have to invoke awk:

Floating Point Operations


expr 10 \* 20

results in 200.

expr 10 + 20

results in 30.

Error Conditions

If a non-integer argument is used, the execution ends in failure (non zero result):

$ expr a + 1
expr: non-integer argument

This behavior is useful to detect whether a variable has an integer value:

How to Tell if a Variable has an Integer Value