Expr: Difference between revisions
Jump to navigation
Jump to search
Line 39: | Line 39: | ||
expr: non-integer argument | expr: non-integer argument | ||
</pre> | </pre> | ||
This behavior is useful to detect whether a variable has an integer value: | |||
{{Internal|Numbers_and_Arithmetic_in_bash#How_to_Tell_if_a_Variable_has_an_Integer_Value|How to Tell if a Variable has an Integer Value}} |
Latest revision as of 21:20, 18 April 2017
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'
To do floating point arithmetic in bash, you'll have to invoke awk:
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
This behavior is useful to detect whether a variable has an integer value: