summaryrefslogtreecommitdiff
path: root/usr/src/man/man1/expr.1
blob: 6fa040869389529e1b113a05e222ca11fe41c846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
.\"
.\" 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
.\" http://www.opengroup.org/bookstore/.
.\"
.\" 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 SunOS 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.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" 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.
.\"
.\" 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.
.\"
.\" 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]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2003, Sun Microsystems, Inc.  All Rights Reserved
.\"
.TH EXPR 1 "Aug 29, 2003"
.SH NAME
expr \- evaluate arguments as an expression
.SH SYNOPSIS
.LP
.nf
\fB/usr/bin/expr\fR \fIargument\fR...
.fi

.LP
.nf
\fB/usr/xpg4/bin/expr\fR \fIargument\fR...
.fi

.LP
.nf
\fB/usr/xpg6/bin/expr\fR \fIargument\fR...
.fi

.SH DESCRIPTION
.SS "/usr/bin/expr, /usr/xpg4/bin/expr"
.sp
.LP
The \fBexpr\fR utility evaluates the expression and writes the result to
standard output. The character \fB0\fR is written to indicate a zero value and
nothing is written to indicate a null string.
.SS "/usr/xpg6/bin/expr"
.sp
.LP
The \fBexpr\fR utility evaluates the expression and writes the result to
standard output followed by a NEWLINE. If there is no result from \fBexpr\fR
processing, a NEWLINE is written to standard output.
.SH OPERANDS
.sp
.LP
The \fIargument\fR operand is evaluated as an expression. Terms of the
expression must be separated by blanks. Characters special to the shell must be
escaped (see \fBsh\fR(1)). Strings containing blanks or other special
characters should be quoted. The length of the expression is limited to
\fBLINE_MAX\fR (2048 characters).
.sp
.LP
The operators and keywords are listed below. The list is in order of increasing
precedence, with equal precedence operators grouped within \fB{\|}\fR symbols.
All of the operators are left-associative.
.sp
.ne 2
.na
\fB\fIexpr\fR \fB\e|\fR \fIexpr\fR\fR
.ad
.sp .6
.RS 4n
Returns the evaluation of the first \fIexpr\fR if it is neither \fINULL\fR nor
\fB0\fR; otherwise, returns the evaluation of the second \fIexpr\fR if it is
not \fINULL\fR; otherwise, \fB0\fR.
.RE

.sp
.ne 2
.na
\fB\fIexpr\fR \fB\e&\fR \fIexpr\fR\fR
.ad
.sp .6
.RS 4n
Returns the first \fIexpr\fR if neither \fIexpr\fR is \fINULL\fR or \fB0\fR,
otherwise returns \fB0\fR.
.RE

.sp
.ne 2
.na
\fB\fIexpr\fR{ \fB=\fR, \fB\e>\fR, \fB\e>=\fR, \fB\e<\fR, \fB\e<=\fR, \fB!=\fR}
\fIexpr\fR\fR
.ad
.sp .6
.RS 4n
Returns the result of an integer comparison if both arguments are integers,
otherwise returns the result of a string comparison using the locale-specific
coalition sequence. The result of each comparison will be \fB1\fR if the
specified relationship is \fBTRUE\fR, \fB0\fR if the relationship is
\fBFALSE\fR.
.RE

.sp
.ne 2
.na
\fB\fIexpr \fR{ \fB+\fR, \fB\(mi \fR} \fIexpr\fR\fR
.ad
.sp .6
.RS 4n
Addition or subtraction of integer-valued arguments.
.RE

.sp
.ne 2
.na
\fB\fIexpr \fR{ \fB\e*\fR, \fB/\fR, \fB%\fR} \fIexpr\fR\fR
.ad
.sp .6
.RS 4n
Multiplication, division, or remainder of the integer-valued arguments.
.RE

.sp
.ne 2
.na
\fB\fIexpr\fR \fB:\fR \fIexpr\fR\fR
.ad
.sp .6
.RS 4n
The matching operator \fB:\fR (colon) compares the first argument with the
second argument, which must be an internationalized basic regular expression
(BRE), 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. See \fBregex\fR(5) and NOTES. Normally, the
\fB/usr/bin/expr\fR matching operator returns the number of bytes matched and
the \fB/usr/xpg4/bin/expr\fR matching operator returns the number of characters
matched (\fB0\fR on failure). If the second argument contains at least one BRE
sub-expression [\e(...\e)], the matching operator returns the string
corresponding to \e1.
.RE

.sp
.ne 2
.na
\fB\fIinteger\fR \fR
.ad
.sp .6
.RS 4n
An argument consisting only of an (optional) unary minus followed by digits.
.RE

.sp
.ne 2
.na
\fB\fIstring\fR \fR
.ad
.sp .6
.RS 4n
A string argument that cannot be identified as an \fIinteger\fR argument or as
one of the expression operator symbols.
.RE

.SS "Compatibility Operators (x86 only)"
.sp
.LP
The following operators are included for compatibility with INTERACTIVE UNIX
System only and are not intended to be used by non- INTERACTIVE UNIX System
scripts:
.sp
.ne 2
.na
\fB\fBindex\fR \fIstring character-list\fR\fR
.ad
.sp .6
.RS 4n
Report the first position in which any one of the bytes in \fIcharacter-list\fR
matches a byte in \fIstring\fR.
.RE

.sp
.ne 2
.na
\fB\fBlength\fR \fIstring\fR\fR
.ad
.sp .6
.RS 4n
Return the length (that is, the number of bytes) of \fIstring\fR.
.RE

.sp
.ne 2
.na
\fB\fBsubstr\fR \fIstring integer-1 integer-2\fR\fR
.ad
.sp .6
.RS 4n
Extract the substring of \fIstring\fR starting at position \fIinteger-1\fR and
of length \fIinteger-2\fR bytes.  If \fIinteger-1\fR has a value greater than
the number of bytes in \fIstring\fR, \fBexpr\fR returns a null string.  If you
try to extract more bytes than there are in string, \fBexpr\fR returns all the
remaining bytes from \fIstring\fR. Results are unspecified if either
\fIinteger-1\fR or \fIinteger-2\fR is a negative value.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fRAdding an integer to a shell variable
.sp
.LP
Add 1 to the shell variable \fBa\fR:

.sp
.in +2
.nf
example$ \fBa=`expr\| $a\| +\| 1`\fR
.fi
.in -2
.sp

.LP
\fBExample 2 \fRReturning a path name segment
.sp
.LP
The following example emulates \fBbasename\fR(1), returning the last segment of
the path name \fB$a\fR. For \fB$a\fR equal to either \fB/usr/abc/file\fR or
just \fBfile\fR, the example returns \fBfile\fR. (Watch out for \fB/\fR alone
as an argument: \fBexpr\fR takes it as the division operator. See NOTES below.)

.sp
.in +2
.nf
example$ \fBexpr $a : '.*/\e(.*\e)' \e| $a\fR
.fi
.in -2
.sp

.LP
\fBExample 3 \fRUsing // characters to simplify the expression
.sp
.LP
Here is a better version of the previous example. The addition of the \fB//\fR
characters eliminates any ambiguity about the division operator and simplifies
the whole expression.

.sp
.in +2
.nf
example$ \fBexpr //$a : '.*/\e(.*\e)'\fR
.fi
.in -2
.sp

.SS "/usr/bin/expr"
.LP
\fBExample 4 \fRReturning the number of bytes in a variable
.sp
.in +2
.nf
example$ \fBexpr "$VAR" : '.*'\fR
.fi
.in -2
.sp

.SS "/usr/xpg4/bin/expr"
.LP
\fBExample 5 \fRReturning the number of characters in a variable
.sp
.in +2
.nf
example$ \fBexpr "$VAR" : '.*'\fR
.fi
.in -2
.sp

.SH ENVIRONMENT VARIABLES
.sp
.LP
See \fBenviron\fR(5) for descriptions of the following environment variables
that affect the execution of \fBexpr\fR: \fBLANG\fR, \fBLC_ALL\fR,
\fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
.SH EXIT STATUS
.sp
.LP
As a side effect of expression evaluation, \fBexpr\fR returns the following
exit values:
.sp
.ne 2
.na
\fB\fB0\fR \fR
.ad
.RS 7n
If the expression is neither \fINULL\fR nor \fB0\fR.
.RE

.sp
.ne 2
.na
\fB\fB1\fR \fR
.ad
.RS 7n
If the expression is either \fINULL\fR or \fB0\fR.
.RE

.sp
.ne 2
.na
\fB\fB2\fR \fR
.ad
.RS 7n
For invalid expressions.
.RE

.sp
.ne 2
.na
\fB\fB>2\fR \fR
.ad
.RS 7n
An error occurred.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
CSI	enabled
_
Interface Stability	Standard
.TE

.SH SEE ALSO
.sp
.LP
\fBbasename\fR(1), \fBed\fR(1), \fBsh\fR(1), \fBIntro\fR(3),
\fBattributes\fR(5), \fBenviron\fR(5), \fBregex\fR(5), \fBstandards\fR(5)
.SH DIAGNOSTICS
.sp
.ne 2
.na
\fB\fBsyntax error\fR\fR
.ad
.RS 24n
Operator and operand errors.
.RE

.sp
.ne 2
.na
\fB\fBnon-numeric argument\fR\fR
.ad
.RS 24n
Arithmetic is attempted on such a string.
.RE

.SH NOTES
.sp
.LP
After argument processing by the shell, \fBexpr\fR cannot tell the difference
between an operator and an operand except by the value. If \fB$a\fR is an
\fB=\fR, the command:
.sp
.in +2
.nf
example$ \fBexpr $a = '='\fR
.fi
.in -2
.sp

.sp
.LP
looks like:
.sp
.in +2
.nf
example$ \fBexpr = = =\fR
.fi
.in -2
.sp

.sp
.LP
as the arguments are passed to \fBexpr\fR (and they are all taken as the
\fB=\fR operator). The following works:
.sp
.in +2
.nf
example$ \fBexpr X$a = X=\fR
.fi
.in -2
.sp

.SS "Regular Expressions"
.sp
.LP
Unlike some previous versions, \fBexpr\fR uses Internationalized Basic Regular
Expressions for all system-provided locales. Internationalized Regular
Expressions are explained on the \fBregex\fR(5) manual page.