2 .\" Copyright 1989 AT&T
3 .\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved
4 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions 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.
8 .\" This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH EXPR 1 "Aug 29, 2003"
14 expr \- evaluate arguments as an expression
18 \fB/usr/bin/expr\fR \fIargument\fR...
23 \fB/usr/xpg4/bin/expr\fR \fIargument\fR...
28 \fB/usr/xpg6/bin/expr\fR \fIargument\fR...
32 .SS "/usr/bin/expr, /usr/xpg4/bin/expr"
35 The \fBexpr\fR utility evaluates the expression and writes the result to
36 standard output. The character \fB0\fR is written to indicate a zero value and
37 nothing is written to indicate a null string.
38 .SS "/usr/xpg6/bin/expr"
41 The \fBexpr\fR utility evaluates the expression and writes the result to
42 standard output followed by a NEWLINE. If there is no result from \fBexpr\fR
43 processing, a NEWLINE is written to standard output.
47 The \fIargument\fR operand is evaluated as an expression. Terms of the
48 expression must be separated by blanks. Characters special to the shell must be
49 escaped (see \fBsh\fR(1)). Strings containing blanks or other special
50 characters should be quoted. The length of the expression is limited to
51 \fBLINE_MAX\fR (2048 characters).
54 The operators and keywords are listed below. The list is in order of increasing
55 precedence, with equal precedence operators grouped within \fB{\|}\fR symbols.
56 All of the operators are left-associative.
60 \fB\fIexpr\fR \fB\e|\fR \fIexpr\fR\fR
64 Returns the evaluation of the first \fIexpr\fR if it is neither \fINULL\fR nor
65 \fB0\fR; otherwise, returns the evaluation of the second \fIexpr\fR if it is
66 not \fINULL\fR; otherwise, \fB0\fR.
72 \fB\fIexpr\fR \fB\e&\fR \fIexpr\fR\fR
76 Returns the first \fIexpr\fR if neither \fIexpr\fR is \fINULL\fR or \fB0\fR,
77 otherwise returns \fB0\fR.
83 \fB\fIexpr\fR{ \fB=\fR, \fB\e>\fR, \fB\e>=\fR, \fB\e<\fR, \fB\e<=\fR, \fB!=\fR}
88 Returns the result of an integer comparison if both arguments are integers,
89 otherwise returns the result of a string comparison using the locale-specific
90 coalition sequence. The result of each comparison will be \fB1\fR if the
91 specified relationship is \fBTRUE\fR, \fB0\fR if the relationship is
98 \fB\fIexpr \fR{ \fB+\fR, \fB\(mi \fR} \fIexpr\fR\fR
102 Addition or subtraction of integer-valued arguments.
108 \fB\fIexpr \fR{ \fB\e*\fR, \fB/\fR, \fB%\fR} \fIexpr\fR\fR
112 Multiplication, division, or remainder of the integer-valued arguments.
118 \fB\fIexpr\fR \fB:\fR \fIexpr\fR\fR
122 The matching operator \fB:\fR (colon) compares the first argument with the
123 second argument, which must be an internationalized basic regular expression
124 (BRE), except that all patterns are anchored to the beginning of the string.
125 That is, only sequences starting at the first character of a string are matched
126 by the regular expression. See \fBregex\fR(5) and NOTES. Normally, the
127 \fB/usr/bin/expr\fR matching operator returns the number of bytes matched and
128 the \fB/usr/xpg4/bin/expr\fR matching operator returns the number of characters
129 matched (\fB0\fR on failure). If the second argument contains at least one BRE
130 sub-expression [\e(...\e)], the matching operator returns the string
131 corresponding to \e1.
141 An argument consisting only of an (optional) unary minus followed by digits.
151 A string argument that cannot be identified as an \fIinteger\fR argument or as
152 one of the expression operator symbols.
155 .SS "Compatibility Operators (x86 only)"
158 The following operators are included for compatibility with INTERACTIVE UNIX
159 System only and are not intended to be used by non- INTERACTIVE UNIX System
164 \fB\fBindex\fR \fIstring character-list\fR\fR
168 Report the first position in which any one of the bytes in \fIcharacter-list\fR
169 matches a byte in \fIstring\fR.
175 \fB\fBlength\fR \fIstring\fR\fR
179 Return the length (that is, the number of bytes) of \fIstring\fR.
185 \fB\fBsubstr\fR \fIstring integer-1 integer-2\fR\fR
189 Extract the substring of \fIstring\fR starting at position \fIinteger-1\fR and
190 of length \fIinteger-2\fR bytes. If \fIinteger-1\fR has a value greater than
191 the number of bytes in \fIstring\fR, \fBexpr\fR returns a null string. If you
192 try to extract more bytes than there are in string, \fBexpr\fR returns all the
193 remaining bytes from \fIstring\fR. Results are unspecified if either
194 \fIinteger-1\fR or \fIinteger-2\fR is a negative value.
199 \fBExample 1 \fRAdding an integer to a shell variable
202 Add 1 to the shell variable \fBa\fR:
207 example$ \fBa=`expr\| $a\| +\| 1`\fR
213 \fBExample 2 \fRReturning a path name segment
216 The following example emulates \fBbasename\fR(1), returning the last segment of
217 the path name \fB$a\fR. For \fB$a\fR equal to either \fB/usr/abc/file\fR or
218 just \fBfile\fR, the example returns \fBfile\fR. (Watch out for \fB/\fR alone
219 as an argument: \fBexpr\fR takes it as the division operator. See NOTES below.)
224 example$ \fBexpr $a : '.*/\e(.*\e)' \e| $a\fR
230 \fBExample 3 \fRUsing // characters to simplify the expression
233 Here is a better version of the previous example. The addition of the \fB//\fR
234 characters eliminates any ambiguity about the division operator and simplifies
235 the whole expression.
240 example$ \fBexpr //$a : '.*/\e(.*\e)'\fR
247 \fBExample 4 \fRReturning the number of bytes in a variable
251 example$ \fBexpr "$VAR" : '.*'\fR
256 .SS "/usr/xpg4/bin/expr"
258 \fBExample 5 \fRReturning the number of characters in a variable
262 example$ \fBexpr "$VAR" : '.*'\fR
267 .SH ENVIRONMENT VARIABLES
270 See \fBenviron\fR(5) for descriptions of the following environment variables
271 that affect the execution of \fBexpr\fR: \fBLANG\fR, \fBLC_ALL\fR,
272 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
276 As a side effect of expression evaluation, \fBexpr\fR returns the following
284 If the expression is neither \fINULL\fR nor \fB0\fR.
293 If the expression is either \fINULL\fR or \fB0\fR.
302 For invalid expressions.
317 See \fBattributes\fR(5) for descriptions of the following attributes:
325 ATTRIBUTE TYPE ATTRIBUTE VALUE
329 Interface Stability Standard
335 \fBbasename\fR(1), \fBed\fR(1), \fBsh\fR(1), \fBIntro\fR(3),
336 \fBattributes\fR(5), \fBenviron\fR(5), \fBregex\fR(5), \fBstandards\fR(5)
341 \fB\fBsyntax error\fR\fR
344 Operator and operand errors.
350 \fB\fBnon-numeric argument\fR\fR
353 Arithmetic is attempted on such a string.
359 After argument processing by the shell, \fBexpr\fR cannot tell the difference
360 between an operator and an operand except by the value. If \fB$a\fR is an
361 \fB=\fR, the command:
365 example$ \fBexpr $a = '='\fR
376 example$ \fBexpr = = =\fR
383 as the arguments are passed to \fBexpr\fR (and they are all taken as the
384 \fB=\fR operator). The following works:
388 example$ \fBexpr X$a = X=\fR
393 .SS "Regular Expressions"
396 Unlike some previous versions, \fBexpr\fR uses Internationalized Basic Regular
397 Expressions for all system-provided locales. Internationalized Regular
398 Expressions are explained on the \fBregex\fR(5) manual page.