Expr: Difference between revisions
Jump to navigation
Jump to search
(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...") |
|||
Line 27: | Line 27: | ||
</pre> | </pre> | ||
results in 200 | results in 200. | ||
<pre> | <pre> | ||
Line 33: | Line 33: | ||
</pre> | </pre> | ||
results in 30 | results in 30. | ||
=Error Conditions= | =Error Conditions= |
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'
This behavior is useful to detect whether a variable has an integer value:
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