The set of arguments passed to expr constitutes an expression to be evaluated. Each command argument is a separate token of the expression. expr writes the result of the expression on the standard output. This command is primarily intended for arithmetic and string manipulation on KornShell variables.
enables the use of octal and hexadecimal numbers when the TK_EXPR_POSIX_COMPLIANT environment variable is set.
expr recognizes the following operators. Operators listed together have equal precedence; otherwise, they are in increasing order of precedence. expr stores expressions as strings and converts them to numbers during the operation. If the context requires a Boolean value, a numeric value of 0 (zero) or a null string ("") is false, and any other value is true. Numbers have an optional leading sign, followed by 0 for octal, 0x for hexadecimal, otherwise decimal, followed by the digits of the number. Numbers are manipulated as long integers.
- expr1 | expr2
results in the value expr1 if expr1 is true; otherwise it results in the value of expr2.
- expr1 & expr2
results in the value of expr1 if both expressions are true; otherwise it results in 0
- expr1 <= expr2
- expr1 < expr2
- expr1 = expr2
- expr1 != expr2
- expr1 >= expr2
- expr1 > expr2
If both expr1 and expr2 are numeric, expr compares them as numbers; otherwise it compares them as strings. If the comparison is true, the expression results in 1; otherwise it results in 0.
- expr1 + expr2
- expr1 - expr2
performs addition or subtraction on the two expressions. If either expression is not a number, expr exits with an error.
- expr1 * expr2
- expr1 / expr2
- expr1 % expr2
performs multiplication, division, or modulus on the two expressions. If either expression is not a number, expr exits with an error. Note that the multiplication symbol (*) is expanded under the KornShell unless you specify it with a leading backslash (\\*), or enclosed in single quotes ('*') or double quotes ("*").
- expr1 : re
- match expr1 re
matches the regular expression re against expr1 treated as a string. The regular expression is the same as that accepted by ed, except that the match is always anchored, that is, there is an implied leading ^; therefore expr does not consider ^ to be a metacharacter. If the regular expression contains \(...\) and it matches at least part of expr1, then expr results in only that part; if there is no match, expr results in 0. If the regular expression doesn't contain this construct, then the result is the number of characters matched. The function match performs the same operation as the colon operator.
- substr expr1 expr2 expr3
results in the substring of expr1 starting at character position expr2 (origin 1) for the length of expr3 characters.
- index expr1 expr2
searches for any of the characters in expr2 in expr1 and returns the first character position (origin 1) at which it finds such a character, or 0 if no such characters are found.
- length expr1
returns the length of expr1 in characters.
- ( expr )
fname=src/fn_abs.c expr $fname : '.*_\(.*\)\.c'
a=`expr $a + 1`
adds one to the value of the shell variable a.
enables POSIX-compliant expr behavior. POSIX-compliant expr does not support octal and hexadecimal numbers, by default. When this variable is set, you can use the
-Wto enable the use of octal and hexadecimal numbers.
Possible exit status values are:
The result of expression is true.
The result of expression is false.
Failure due to any of following:
- internal tree error
Syntax errors or unusual expression complexity make it impossible for expr to evaluate an expression. If an expression has syntax errors, correct them; if not, simplify the expression (perhaps by breaking it into parts).
POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows XP. Windows Server 2003. Windows Vista. Windows 7. Windows Server 2008. Windows 8. Windows Server 2012.
Under cmd.exe, it is difficult to enter certain characters such as > or |) significant to expr as these have special meanings to that command interpreters.
On XP/2003/Vista/7/2008/8/2012, under cmd.exe, you may use the escape character ^ (caret) to prevent the command interpreter from interpreting these special characters, for example:
expr 1 ^> 2 returns a value of 0
In the KornShell, let largely supersedes this command.
match, substr, length, and index
are undocumented on all UNIX systems, though they do appear to exist there.
They are extensions to the POSIX and x/OPEN standards.
The parser stack depth is limited to 150 levels. Attempting to process extremely complicated expressions may result in an overflow of this stack, causing an error.
PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC MKS Toolkit 9.6 patch 1 Documentation Build 5.