expr
Evaluate arguments as an expression (POSIX, toybox)
Syntax:
expr arg1 operator arg2...
Runs on:
QNX OS
Options
- arg1
- The first argument to be evaluated.
- arg2
- The second argument to be evaluated.
- operator
- The operator that performs the specified arithmetic operations.
Description:
The expr utility evaluates a single expression and writes the result to
standard output (e.g., expr 1 + 2
prints 3
). The
expression is formed from integer and string symbols in combination with the following
operators:
( ) : * / % + - != <= < >= > = & |
In the following table, expressions are listed in order of decreasing precedence, with equal-precedence operators grouped together.
Expression | Description |
---|---|
( expr ) | Grouping symbols; you can place any expression inside the parentheses. |
arg1 : arg2 | Matching expression |
arg1 * arg2 | Integer multiplication |
arg1 / arg2 | Integer division, producing an integer result |
arg1 % arg2 | Remainder of integer division |
arg1 + arg2 | Integer addition |
arg1 - arg2 | Integer subtraction |
arg1 = arg2 | Equal* |
arg1 > arg2 | Greater than* |
arg1 >= arg2 | Greater than or equal* |
arg1 < arg2 | Less than* |
arg1 <= arg2 | Less than or equal* |
arg1 != arg2 | Not equal* |
arg1 & arg2 | Returns the evaluation of expr1 if neither expression evaluates to null or zero; otherwise, returns zero |
arg1 | arg2 | Returns the evaluation of expr1 if it's neither null nor zero; otherwise returns the evaluation of expr2 |
* Returns the result of a decimal integer comparison if both arguments are integers; otherwise, returns the result of a string comparison. The result of each comparison is one if the specified relation is TRUE or zero if FALSE.
Each constant and operator must be a separate command line argument. All operators are infix—they require a value on each side of the operator. Operators of the same priority are evaluated from left to right. Parentheses elevate the priority of the expressions that they contain. The & and | operators are logical (not bitwise).
All operators yield integers, and most operators expect integer arguments. Comparisons may
alphabetically compare strings, logical operators treat a blank string as false and nonblank
as true, and the regex operator (str : pattern) yields
the initial number of matching bytes. (So abc : ab
is 2, but abc :
bc
is 0.)
Many of the operators are also shell control operators or reserved words and have to be escaped on the command line. Thus, in many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr.
The &
and |
operators are logical (not bitwise) and
may operate on strings (e.g., a blank string is "false"). Comparison operators may also
operate on strings (in alphabetical sort).
Constants may be strings or integers. Comparison, logical, and regular expression operators may operate on strings while other operators require integers.
This utility is provided as part of the toybox package. For information on how to enable it, see toybox.
Caveats:
The syntax for expr requires special attention. Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line. In addition, each part of the expression is composed of separate arguments, so you should use blanks liberally. For example:
Instead of entering: | You should enter: |
---|---|
expr "1 + 2" | expr 1 + 2 |
expr 1 + (2 * 3) | expr 1 + \( 2 \* 3 \) |
expr 1+2 | expr 1 + 2 |
In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. However, you may need expr to run older UNIX shell scripts.
When you want to specify the filesystem root, avoid the /
character as a
standalone argument—expr interprets it as the division operator.
Use //
instead.
Contributing author:
Rob Landley and the toybox project (see https://landley.net/toybox/).