expr

evaluate expression 

Command


SYNOPSIS

expr [-W] expression


DESCRIPTION

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.

Options

-W 

enables the use of octal and hexadecimal numbers when the TK_EXPR_POSIX_COMPLIANT environment variable is set.

Operators

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

groups expressions.


EXAMPLES

fname=src/fn_abs.c
expr $fname : '.*_\(.*\)\.c'

returns abs.

a=`expr $a + 1`

adds one to the value of the shell variable a.


ENVIRONMENT VARIABLES

TK_EXPR_POSIX_COMPLIANT 

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 -W to enable the use of octal and hexadecimal numbers.


DIAGNOSTICS

Possible exit status values are:

0 

The result of expression is true.

1 

The result of expression is false.

2 

Failure due to any of following:

— insufficient memory
— command line syntax error
— too few arguments on the command line
— invalid regular expression
— regular expression is too complicated
— non-numeric value found where a number was expected

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).


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.

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 8.1/2012R2/10/2016/2019/11/2022, 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 -W option is an extension to both the POSIX and x/OPEN standards.


LIMITS

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.


AVAILABILITY

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 Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
ed, let, sh, test

Miscellaneous:
regexp


PTC MKS Toolkit 10.4 Documentation Build 39.