.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved .TH "EXPR" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual" .\" expr .SH NAME expr \- evaluate arguments as an expression .SH SYNOPSIS .LP \fBexpr\fP \fIoperand\fP .SH DESCRIPTION .LP The \fIexpr\fP utility shall evaluate an expression and write the result to standard output. .SH OPTIONS .LP None. .SH OPERANDS .LP The single expression evaluated by \fIexpr\fP shall be formed from the operands, as described in the EXTENDED DESCRIPTION section. The application shall ensure that each of the expression operator symbols: .sp .RS .nf \fB( ) | & = > >= < <= != + - * / % : \fP .fi .RE .LP and the symbols \fIinteger\fP and \fIstring\fP in the table are provided as separate arguments to \fIexpr\fP. .SH STDIN .LP Not used. .SH INPUT FILES .LP None. .SH ENVIRONMENT VARIABLES .LP The following environment variables shall affect the execution of \fIexpr\fP: .TP 7 \fILANG\fP Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of IEEE\ Std\ 1003.1-2001, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) .TP 7 \fILC_ALL\fP If set to a non-empty string value, override the values of all the other internationalization variables. .TP 7 \fILC_COLLATE\fP .sp Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators. .TP 7 \fILC_CTYPE\fP Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments) and the behavior of character classes within regular expressions. .TP 7 \fILC_MESSAGES\fP Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. .TP 7 \fINLSPATH\fP Determine the location of message catalogs for the processing of \fILC_MESSAGES \&.\fP .sp .SH ASYNCHRONOUS EVENTS .LP Default. .SH STDOUT .LP The \fIexpr\fP utility shall evaluate the expression and write the result, followed by a , to standard output. .SH STDERR .LP The standard error shall be used only for diagnostic messages. .SH OUTPUT FILES .LP None. .SH EXTENDED DESCRIPTION .LP The formation of the expression to be evaluated is shown in the following table. The symbols \fIexpr\fP, \fIexpr1\fP, and \fIexpr2\fP represent expressions formed from \fIinteger\fP and \fIstring\fP symbols and the expression operator symbols (all separate arguments) by recursive application of the constructs described in the table. The expressions are listed in order of increasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be left-associative. .TS C center; l lw(40). \fBExpression\fP T{ .na \fBDescription\fP .ad T} \fIexpr1\fP\ |\ \fIexpr2\fP T{ .na Returns the evaluation of \fIexpr1\fP if it is neither null nor zero; otherwise, returns the evaluation of \fIexpr2\fP if it is not null; otherwise, zero. .ad T} \fIexpr1\fP\ &\ \fIexpr2\fP T{ .na Returns the evaluation of \fIexpr1\fP if neither expression evaluates to null or zero; otherwise, returns zero. .ad T} \ T{ .na Returns the result of a decimal integer comparison if both arguments are integers; otherwise, returns the result of a string comparison using the locale-specific collation sequence. The result of each comparison is 1 if the specified relationship is true, or 0 if the relationship is false. .ad T} \fIexpr1\fP\ =\ \fIexpr2\fP T{ .na Equal. .ad T} \fIexpr1\fP\ >\ \fIexpr2\fP T{ .na Greater than. .ad T} \fIexpr1\fP\ >=\ \fIexpr2\fP T{ .na Greater than or equal. .ad T} \fIexpr1\fP\ <\ \fIexpr2\fP T{ .na Less than. .ad T} \fIexpr1\fP\ <=\ \fIexpr2\fP T{ .na Less than or equal. .ad T} \fIexpr1\fP\ !=\ \fIexpr2\fP T{ .na Not equal. .ad T} \fIexpr1\fP\ +\ \fIexpr2\fP T{ .na Addition of decimal integer-valued arguments. .ad T} \fIexpr1\fP\ -\ \fIexpr2\fP T{ .na Subtraction of decimal integer-valued arguments. .ad T} \fIexpr1\fP\ *\ \fIexpr2\fP T{ .na Multiplication of decimal integer-valued arguments. .ad T} \fIexpr1\fP\ /\ \fIexpr2\fP T{ .na Integer division of decimal integer-valued arguments, producing an integer result. .ad T} \fIexpr1\fP\ %\ \fIexpr2\fP T{ .na Remainder of integer division of decimal integer-valued arguments. .ad T} \fIexpr1\fP\ :\ \fIexpr2\fP T{ .na Matching expression; see below. .ad T} (\ \fIexpr\fP\ ) T{ .na Grouping symbols. Any expression can be placed within parentheses. Parentheses can be nested to a depth of {EXPR_NEST_MAX}. .ad T} \fIinteger\fP T{ .na An argument consisting only of an (optional) unary minus followed by digits. .ad T} \fIstring\fP T{ .na A string argument; see below. .ad T} .TE .SS Matching Expression .LP The \fB':'\fP matching operator shall compare the string resulting from the evaluation of \fIexpr1\fP with the regular expression pattern resulting from the evaluation of \fIexpr2\fP. Regular expression syntax shall be that defined in the Base Definitions volume of IEEE\ Std\ 1003.1-2001, Section 9.3, Basic Regular Expressions, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the first character of a string are matched by the regular expression) and, therefore, it is unspecified whether \fB'^'\fP is a special character in that context. Usually, the matching operator shall return a string representing the number of characters matched ( \fB'0'\fP on failure). Alternatively, if the pattern contains at least one regular expression subexpression \fB"[\\(...\\)]"\fP , the string corresponding to \fB"\\1"\fP shall be returned. .SS String Operand .LP A string argument is an argument that cannot be identified as an \fIinteger\fP argument or as one of the expression operator symbols shown in the OPERANDS section. .LP The use of string arguments \fBlength\fP, \fBsubstr\fP, \fBindex\fP, or \fBmatch\fP produces unspecified results. .SH EXIT STATUS .LP The following exit values shall be returned: .TP 7 \ 0 The \fIexpression\fP evaluates to neither null nor zero. .TP 7 \ 1 The \fIexpression\fP evaluates to null or zero. .TP 7 \ 2 Invalid \fIexpression\fP. .TP 7 >2 An error occurred. .sp .SH CONSEQUENCES OF ERRORS .LP Default. .LP \fIThe following sections are informative.\fP .SH APPLICATION USAGE .LP After argument processing by the shell, \fIexpr\fP is not required to be able to tell the difference between an operator and an operand except by the value. If \fB"$a"\fP is \fB'='\fP , the command: .sp .RS .nf \fBexpr $a = '=' \fP .fi .RE .LP looks like: .sp .RS .nf \fBexpr = = = \fP .fi .RE .LP as the arguments are passed to \fIexpr\fP (and they all may be taken as the \fB'='\fP operator). The following works reliably: .sp .RS .nf \fBexpr X$a = X= \fP .fi .RE .LP Also note that this volume of IEEE\ Std\ 1003.1-2001 permits implementations to extend utilities. The \fIexpr\fP utility permits the integer arguments to be preceded with a unary minus. This means that an integer argument could look like an option. Therefore, the conforming application must employ the \fB"--"\fP construct of Guideline 10 of the Base Definitions volume of IEEE\ Std\ 1003.1-2001, Section 12.2, Utility Syntax Guidelines to protect its operands if there is any chance the first operand might be a negative integer (or any string with a leading minus). .SH EXAMPLES .LP The \fIexpr\fP utility has a rather difficult syntax: .IP " *" 3 Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line. .LP .IP " *" 3 Each part of the expression is composed of separate arguments, so liberal usage of s is required. For example: .TS C center; l l. \fBInvalid\fP \fBValid\fP \fIexpr\fP 1+2 \fIexpr\fP 1 + 2 \fIexpr\fP "1 + 2" \fIexpr\fP 1 + 2 \fIexpr\fP 1 + (2 * 3) \fIexpr\fP 1 + \\( 2 \\* 3 \\) .TE .LP .LP In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in \fIexpr\fP. Newly written scripts should avoid \fIexpr\fP in favor of the new features within the shell; see \fIParameters and Variables\fP and \fIArithmetic Expansion\fP . .LP The following command: .sp .RS .nf \fBa=$(expr $a + 1) \fP .fi .RE .LP adds 1 to the variable \fIa\fP. .LP The following command, for \fB"$a"\fP equal to either \fB/usr/abc/file\fP or just \fBfile\fP: .sp .RS .nf \fBexpr $a : '.*/\\(.*\\)' \\| $a \fP .fi .RE .LP returns the last segment of a pathname (that is, \fBfile\fP). Applications should avoid the character \fB'/'\fP used alone as an argument; \fIexpr\fP may interpret it as the division operator. .LP The following command: .sp .RS .nf \fBexpr "//$a" : '.*/\\(.*\\)' \fP .fi .RE .LP is a better representation of the previous example. The addition of the \fB"//"\fP characters eliminates any ambiguity about the division operator and simplifies the whole expression. Also note that pathnames may contain characters contained in the \fIIFS\fP variable and should be quoted to avoid having \fB"$a"\fP expand into multiple arguments. .LP The following command: .sp .RS .nf \fBexpr "$VAR" : '.*' \fP .fi .RE .LP returns the number of characters in \fIVAR\fP. .SH RATIONALE .LP In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historical applications. .LP The use of a leading circumflex in the BRE is unspecified because many historical implementations have treated it as a special character, despite their system documentation. For example: .sp .RS .nf \fBexpr foo : ^foo expr ^foo : ^foo \fP .fi .RE .LP return 3 and 0, respectively, on those systems; their documentation would imply the reverse. Thus, the anchoring condition is left unspecified to avoid breaking historical scripts relying on this undocumented feature. .SH FUTURE DIRECTIONS .LP None. .SH SEE ALSO .LP \fIParameters and Variables\fP , \fIArithmetic Expansion\fP .SH COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .