Expr

From NovaOrdis Knowledge Base
Revision as of 21:18, 18 April 2017 by Ovidiu (talk | contribs) (Created page with "=Internal= * Linux =Overview= "expr" evaluates an expression and prints the value of that expression at stdout. All operators and operands must be passed...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

"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'

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

How to Tell if a Variable has an Integer Value

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

Floating Point Operations

Examples

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