expr: Evaluate expressions
expr evaluates an expression and writes the result on standard
output. Each token of the expression must be a separate argument.
Operands are either integers or strings. Integers consist of one or
more decimal digits, with an optional leading ‘-’.
anything appearing in an operand position to an integer or a string
depending on the operation being applied to it.
Strings are not quoted for
expr itself, though you may need to
quote them to protect characters with special meaning to the shell,
e.g., spaces. However, regardless of whether it is quoted, a string
operand should not be a parenthesis or any of
+, so you cannot safely pass an arbitrary string
$str to expr merely by quoting it to the shell. One way to
work around this is to use the GNU extension
+ "$str" = foo); a more portable way is to use
" $str" and to adjust the rest of the expression to take
the leading space into account (e.g.,
" $str" = " foo").
You should not pass a negative integer or a string with leading
expr’s first argument, as it might be
misinterpreted as an option; this can be avoided by parenthesization.
Also, portable scripts should not use a string operand that happens to
take the form of an integer; this can be worked around by inserting
leading spaces as mentioned above.
Operators may be given as infix symbols or prefix keywords. Parentheses may be used for grouping in the usual manner. You must quote parentheses and many operators to avoid the shell evaluating them, however.
When built with support for the GNU MP library,
arbitrary-precision arithmetic; otherwise, it uses native arithmetic
types and may fail due to arithmetic overflow.
The only options are --help and --version. See Common options. Options must precede operands.
0 if the expression is neither null nor 0, 1 if the expression is null or 0, 2 if the expression is invalid, 3 if an internal error occurred (e.g., arithmetic overflow).
|• String expressions||+ : match substr index length|
|• Numeric expressions||+ - * / %|
|• Relations for expr||| & < <= = == != >= >|
|• Examples of expr||Examples.|