summaryrefslogtreecommitdiffstats
path: root/man1p/expr.1p
blob: 2e16fde1c31201fbaa7bec9749882b583843def0 (plain) (blame)
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
.\" 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 <newline>, 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 <blank>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 .