Expr: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
expr: not a decimal number: '10.1'
expr: not a decimal number: '10.1'
</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}}


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

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:

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