9103 opengroup acknowledgement should be properly formatted in man pages
[unleashed.git] / usr / src / man / man1 / expr.1
blob6fa040869389529e1b113a05e222ca11fe41c846
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2003, Sun Microsystems, Inc.  All Rights Reserved
46 .\"
47 .TH EXPR 1 "Aug 29, 2003"
48 .SH NAME
49 expr \- evaluate arguments as an expression
50 .SH SYNOPSIS
51 .LP
52 .nf
53 \fB/usr/bin/expr\fR \fIargument\fR...
54 .fi
56 .LP
57 .nf
58 \fB/usr/xpg4/bin/expr\fR \fIargument\fR...
59 .fi
61 .LP
62 .nf
63 \fB/usr/xpg6/bin/expr\fR \fIargument\fR...
64 .fi
66 .SH DESCRIPTION
67 .SS "/usr/bin/expr, /usr/xpg4/bin/expr"
68 .sp
69 .LP
70 The \fBexpr\fR utility evaluates the expression and writes the result to
71 standard output. The character \fB0\fR is written to indicate a zero value and
72 nothing is written to indicate a null string.
73 .SS "/usr/xpg6/bin/expr"
74 .sp
75 .LP
76 The \fBexpr\fR utility evaluates the expression and writes the result to
77 standard output followed by a NEWLINE. If there is no result from \fBexpr\fR
78 processing, a NEWLINE is written to standard output.
79 .SH OPERANDS
80 .sp
81 .LP
82 The \fIargument\fR operand is evaluated as an expression. Terms of the
83 expression must be separated by blanks. Characters special to the shell must be
84 escaped (see \fBsh\fR(1)). Strings containing blanks or other special
85 characters should be quoted. The length of the expression is limited to
86 \fBLINE_MAX\fR (2048 characters).
87 .sp
88 .LP
89 The operators and keywords are listed below. The list is in order of increasing
90 precedence, with equal precedence operators grouped within \fB{\|}\fR symbols.
91 All of the operators are left-associative.
92 .sp
93 .ne 2
94 .na
95 \fB\fIexpr\fR \fB\e|\fR \fIexpr\fR\fR
96 .ad
97 .sp .6
98 .RS 4n
99 Returns the evaluation of the first \fIexpr\fR if it is neither \fINULL\fR nor
100 \fB0\fR; otherwise, returns the evaluation of the second \fIexpr\fR if it is
101 not \fINULL\fR; otherwise, \fB0\fR.
105 .ne 2
107 \fB\fIexpr\fR \fB\e&\fR \fIexpr\fR\fR
109 .sp .6
110 .RS 4n
111 Returns the first \fIexpr\fR if neither \fIexpr\fR is \fINULL\fR or \fB0\fR,
112 otherwise returns \fB0\fR.
116 .ne 2
118 \fB\fIexpr\fR{ \fB=\fR, \fB\e>\fR, \fB\e>=\fR, \fB\e<\fR, \fB\e<=\fR, \fB!=\fR}
119 \fIexpr\fR\fR
121 .sp .6
122 .RS 4n
123 Returns the result of an integer comparison if both arguments are integers,
124 otherwise returns the result of a string comparison using the locale-specific
125 coalition sequence. The result of each comparison will be \fB1\fR if the
126 specified relationship is \fBTRUE\fR, \fB0\fR if the relationship is
127 \fBFALSE\fR.
131 .ne 2
133 \fB\fIexpr \fR{ \fB+\fR, \fB\(mi \fR} \fIexpr\fR\fR
135 .sp .6
136 .RS 4n
137 Addition or subtraction of integer-valued arguments.
141 .ne 2
143 \fB\fIexpr \fR{ \fB\e*\fR, \fB/\fR, \fB%\fR} \fIexpr\fR\fR
145 .sp .6
146 .RS 4n
147 Multiplication, division, or remainder of the integer-valued arguments.
151 .ne 2
153 \fB\fIexpr\fR \fB:\fR \fIexpr\fR\fR
155 .sp .6
156 .RS 4n
157 The matching operator \fB:\fR (colon) compares the first argument with the
158 second argument, which must be an internationalized basic regular expression
159 (BRE), except that all patterns are anchored to the beginning of the string.
160 That is, only sequences starting at the first character of a string are matched
161 by the regular expression. See \fBregex\fR(5) and NOTES. Normally, the
162 \fB/usr/bin/expr\fR matching operator returns the number of bytes matched and
163 the \fB/usr/xpg4/bin/expr\fR matching operator returns the number of characters
164 matched (\fB0\fR on failure). If the second argument contains at least one BRE
165 sub-expression [\e(...\e)], the matching operator returns the string
166 corresponding to \e1.
170 .ne 2
172 \fB\fIinteger\fR \fR
174 .sp .6
175 .RS 4n
176 An argument consisting only of an (optional) unary minus followed by digits.
180 .ne 2
182 \fB\fIstring\fR \fR
184 .sp .6
185 .RS 4n
186 A string argument that cannot be identified as an \fIinteger\fR argument or as
187 one of the expression operator symbols.
190 .SS "Compatibility Operators (x86 only)"
193 The following operators are included for compatibility with INTERACTIVE UNIX
194 System only and are not intended to be used by non- INTERACTIVE UNIX System
195 scripts:
197 .ne 2
199 \fB\fBindex\fR \fIstring character-list\fR\fR
201 .sp .6
202 .RS 4n
203 Report the first position in which any one of the bytes in \fIcharacter-list\fR
204 matches a byte in \fIstring\fR.
208 .ne 2
210 \fB\fBlength\fR \fIstring\fR\fR
212 .sp .6
213 .RS 4n
214 Return the length (that is, the number of bytes) of \fIstring\fR.
218 .ne 2
220 \fB\fBsubstr\fR \fIstring integer-1 integer-2\fR\fR
222 .sp .6
223 .RS 4n
224 Extract the substring of \fIstring\fR starting at position \fIinteger-1\fR and
225 of length \fIinteger-2\fR bytes.  If \fIinteger-1\fR has a value greater than
226 the number of bytes in \fIstring\fR, \fBexpr\fR returns a null string.  If you
227 try to extract more bytes than there are in string, \fBexpr\fR returns all the
228 remaining bytes from \fIstring\fR. Results are unspecified if either
229 \fIinteger-1\fR or \fIinteger-2\fR is a negative value.
232 .SH EXAMPLES
234 \fBExample 1 \fRAdding an integer to a shell variable
237 Add 1 to the shell variable \fBa\fR:
240 .in +2
242 example$ \fBa=`expr\| $a\| +\| 1`\fR
244 .in -2
248 \fBExample 2 \fRReturning a path name segment
251 The following example emulates \fBbasename\fR(1), returning the last segment of
252 the path name \fB$a\fR. For \fB$a\fR equal to either \fB/usr/abc/file\fR or
253 just \fBfile\fR, the example returns \fBfile\fR. (Watch out for \fB/\fR alone
254 as an argument: \fBexpr\fR takes it as the division operator. See NOTES below.)
257 .in +2
259 example$ \fBexpr $a : '.*/\e(.*\e)' \e| $a\fR
261 .in -2
265 \fBExample 3 \fRUsing // characters to simplify the expression
268 Here is a better version of the previous example. The addition of the \fB//\fR
269 characters eliminates any ambiguity about the division operator and simplifies
270 the whole expression.
273 .in +2
275 example$ \fBexpr //$a : '.*/\e(.*\e)'\fR
277 .in -2
280 .SS "/usr/bin/expr"
282 \fBExample 4 \fRReturning the number of bytes in a variable
284 .in +2
286 example$ \fBexpr "$VAR" : '.*'\fR
288 .in -2
291 .SS "/usr/xpg4/bin/expr"
293 \fBExample 5 \fRReturning the number of characters in a variable
295 .in +2
297 example$ \fBexpr "$VAR" : '.*'\fR
299 .in -2
302 .SH ENVIRONMENT VARIABLES
305 See \fBenviron\fR(5) for descriptions of the following environment variables
306 that affect the execution of \fBexpr\fR: \fBLANG\fR, \fBLC_ALL\fR,
307 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
308 .SH EXIT STATUS
311 As a side effect of expression evaluation, \fBexpr\fR returns the following
312 exit values:
314 .ne 2
316 \fB\fB0\fR \fR
318 .RS 7n
319 If the expression is neither \fINULL\fR nor \fB0\fR.
323 .ne 2
325 \fB\fB1\fR \fR
327 .RS 7n
328 If the expression is either \fINULL\fR or \fB0\fR.
332 .ne 2
334 \fB\fB2\fR \fR
336 .RS 7n
337 For invalid expressions.
341 .ne 2
343 \fB\fB>2\fR \fR
345 .RS 7n
346 An error occurred.
349 .SH ATTRIBUTES
352 See \fBattributes\fR(5) for descriptions of the following attributes:
357 box;
358 c | c
359 l | l .
360 ATTRIBUTE TYPE  ATTRIBUTE VALUE
362 CSI     enabled
364 Interface Stability     Standard
367 .SH SEE ALSO
370 \fBbasename\fR(1), \fBed\fR(1), \fBsh\fR(1), \fBIntro\fR(3),
371 \fBattributes\fR(5), \fBenviron\fR(5), \fBregex\fR(5), \fBstandards\fR(5)
372 .SH DIAGNOSTICS
374 .ne 2
376 \fB\fBsyntax error\fR\fR
378 .RS 24n
379 Operator and operand errors.
383 .ne 2
385 \fB\fBnon-numeric argument\fR\fR
387 .RS 24n
388 Arithmetic is attempted on such a string.
391 .SH NOTES
394 After argument processing by the shell, \fBexpr\fR cannot tell the difference
395 between an operator and an operand except by the value. If \fB$a\fR is an
396 \fB=\fR, the command:
398 .in +2
400 example$ \fBexpr $a = '='\fR
402 .in -2
407 looks like:
409 .in +2
411 example$ \fBexpr = = =\fR
413 .in -2
418 as the arguments are passed to \fBexpr\fR (and they are all taken as the
419 \fB=\fR operator). The following works:
421 .in +2
423 example$ \fBexpr X$a = X=\fR
425 .in -2
428 .SS "Regular Expressions"
431 Unlike some previous versions, \fBexpr\fR uses Internationalized Basic Regular
432 Expressions for all system-provided locales. Internationalized Regular
433 Expressions are explained on the \fBregex\fR(5) manual page.