1EXPR(1P) POSIX Programmer's Manual EXPR(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 expr - evaluate arguments as an expression
13
15 expr operand
16
18 The expr utility shall evaluate an expression and write the result to
19 standard output.
20
22 None.
23
25 The single expression evaluated by expr shall be formed from the oper‐
26 ands, as described in the EXTENDED DESCRIPTION section. The application
27 shall ensure that each of the expression operator symbols:
28
29
30 ( ) | & = > >= < <= != + - * / % :
31
32 and the symbols integer and string in the table are provided as sepa‐
33 rate arguments to expr.
34
36 Not used.
37
39 None.
40
42 The following environment variables shall affect the execution of expr:
43
44 LANG Provide a default value for the internationalization variables
45 that are unset or null. (See the Base Definitions volume of
46 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
47 ables for the precedence of internationalization variables used
48 to determine the values of locale categories.)
49
50 LC_ALL If set to a non-empty string value, override the values of all
51 the other internationalization variables.
52
53 LC_COLLATE
54
55 Determine the locale for the behavior of ranges, equivalence
56 classes, and multi-character collating elements within regular
57 expressions and by the string comparison operators.
58
59 LC_CTYPE
60 Determine the locale for the interpretation of sequences of
61 bytes of text data as characters (for example, single-byte as
62 opposed to multi-byte characters in arguments) and the behavior
63 of character classes within regular expressions.
64
65 LC_MESSAGES
66 Determine the locale that should be used to affect the format
67 and contents of diagnostic messages written to standard error.
68
69 NLSPATH
70 Determine the location of message catalogs for the processing of
71 LC_MESSAGES .
72
73
75 Default.
76
78 The expr utility shall evaluate the expression and write the result,
79 followed by a <newline>, to standard output.
80
82 The standard error shall be used only for diagnostic messages.
83
85 None.
86
88 The formation of the expression to be evaluated is shown in the follow‐
89 ing table. The symbols expr, expr1, and expr2 represent expressions
90 formed from integer and string symbols and the expression operator sym‐
91 bols (all separate arguments) by recursive application of the con‐
92 structs described in the table. The expressions are listed in order of
93 increasing precedence, with equal-precedence operators grouped between
94 horizontal lines. All of the operators shall be left-associative.
95
96 Expression Description
97 expr1 | expr2 Returns the evaluation of expr1 if it is
98 neither null nor zero; otherwise,
99 returns the evaluation of expr2 if it is
100 not null; otherwise, zero.
101 expr1 & expr2 Returns the evaluation of expr1 if nei‐
102 ther expression evaluates to null or
103 zero; otherwise, returns zero.
104 Returns the result of a decimal integer
105 comparison if both arguments are inte‐
106 gers; otherwise, returns the result of a
107 string comparison using the locale-spe‐
108 cific collation sequence. The result of
109 each comparison is 1 if the specified
110 relationship is true, or 0 if the rela‐
111 tionship is false.
112 expr1 = expr2 Equal.
113 expr1 > expr2 Greater than.
114 expr1 >= expr2 Greater than or equal.
115 expr1 < expr2 Less than.
116 expr1 <= expr2 Less than or equal.
117 expr1 != expr2 Not equal.
118 expr1 + expr2 Addition of decimal integer-valued argu‐
119 ments.
120 expr1 - expr2 Subtraction of decimal integer-valued
121 arguments.
122 expr1 * expr2 Multiplication of decimal integer-valued
123 arguments.
124 expr1 / expr2 Integer division of decimal integer-val‐
125 ued arguments, producing an integer
126 result.
127 expr1 % expr2 Remainder of integer division of decimal
128 integer-valued arguments.
129 expr1 : expr2 Matching expression; see below.
130 ( expr ) Grouping symbols. Any expression can be
131 placed within parentheses. Parentheses
132 can be nested to a depth of
133 {EXPR_NEST_MAX}.
134
135 integer An argument consisting only of an
136 (optional) unary minus followed by dig‐
137 its.
138 string A string argument; see below.
139
140 Matching Expression
141 The ':' matching operator shall compare the string resulting from the
142 evaluation of expr1 with the regular expression pattern resulting from
143 the evaluation of expr2. Regular expression syntax shall be that
144 defined in the Base Definitions volume of IEEE Std 1003.1-2001, Section
145 9.3, Basic Regular Expressions, except that all patterns are anchored
146 to the beginning of the string (that is, only sequences starting at the
147 first character of a string are matched by the regular expression) and,
148 therefore, it is unspecified whether '^' is a special character in that
149 context. Usually, the matching operator shall return a string repre‐
150 senting the number of characters matched ( '0' on failure). Alterna‐
151 tively, if the pattern contains at least one regular expression subex‐
152 pression "[\(...\)]", the string corresponding to "\1" shall be
153 returned.
154
155 String Operand
156 A string argument is an argument that cannot be identified as an inte‐
157 ger argument or as one of the expression operator symbols shown in the
158 OPERANDS section.
159
160 The use of string arguments length, substr, index, or match produces
161 unspecified results.
162
164 The following exit values shall be returned:
165
166 0 The expression evaluates to neither null nor zero.
167
168 1 The expression evaluates to null or zero.
169
170 2 Invalid expression.
171
172 >2 An error occurred.
173
174
176 Default.
177
178 The following sections are informative.
179
181 After argument processing by the shell, expr is not required to be able
182 to tell the difference between an operator and an operand except by the
183 value. If "$a" is '=', the command:
184
185
186 expr $a = '='
187
188 looks like:
189
190
191 expr = = =
192
193 as the arguments are passed to expr (and they all may be taken as the
194 '=' operator). The following works reliably:
195
196
197 expr X$a = X=
198
199 Also note that this volume of IEEE Std 1003.1-2001 permits implementa‐
200 tions to extend utilities. The expr utility permits the integer argu‐
201 ments to be preceded with a unary minus. This means that an integer
202 argument could look like an option. Therefore, the conforming applica‐
203 tion must employ the "--" construct of Guideline 10 of the Base Defini‐
204 tions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax
205 Guidelines to protect its operands if there is any chance the first op‐
206 erand might be a negative integer (or any string with a leading minus).
207
209 The expr utility has a rather difficult syntax:
210
211 * Many of the operators are also shell control operators or reserved
212 words, so they have to be escaped on the command line.
213
214 * Each part of the expression is composed of separate arguments, so
215 liberal usage of <blank>s is required. For example:
216
217 Invalid Valid
218 expr 1+2 expr 1 + 2
219 expr "1 + 2" expr 1 + 2
220 expr 1 + (2 * 3) expr 1 + \( 2 \* 3 \)
221
222 In many cases, the arithmetic and string features provided as part of
223 the shell command language are easier to use than their equivalents in
224 expr. Newly written scripts should avoid expr in favor of the new fea‐
225 tures within the shell; see Parameters and Variables and Arithmetic
226 Expansion .
227
228 The following command:
229
230
231 a=$(expr $a + 1)
232
233 adds 1 to the variable a.
234
235 The following command, for "$a" equal to either /usr/abc/file or just
236 file:
237
238
239 expr $a : '.*/\(.*\)' \| $a
240
241 returns the last segment of a pathname (that is, file). Applications
242 should avoid the character '/' used alone as an argument; expr may
243 interpret it as the division operator.
244
245 The following command:
246
247
248 expr "//$a" : '.*/\(.*\)'
249
250 is a better representation of the previous example. The addition of the
251 "//" characters eliminates any ambiguity about the division operator
252 and simplifies the whole expression. Also note that pathnames may con‐
253 tain characters contained in the IFS variable and should be quoted to
254 avoid having "$a" expand into multiple arguments.
255
256 The following command:
257
258
259 expr "$VAR" : '.*'
260
261 returns the number of characters in VAR.
262
264 In an early proposal, EREs were used in the matching expression syntax.
265 This was changed to BREs to avoid breaking historical applications.
266
267 The use of a leading circumflex in the BRE is unspecified because many
268 historical implementations have treated it as a special character,
269 despite their system documentation. For example:
270
271
272 expr foo : ^foo expr ^foo : ^foo
273
274 return 3 and 0, respectively, on those systems; their documentation
275 would imply the reverse. Thus, the anchoring condition is left unspeci‐
276 fied to avoid breaking historical scripts relying on this undocumented
277 feature.
278
280 None.
281
283 Parameters and Variables, Arithmetic Expansion
284
286 Portions of this text are reprinted and reproduced in electronic form
287 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
288 -- Portable Operating System Interface (POSIX), The Open Group Base
289 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
290 Electrical and Electronics Engineers, Inc and The Open Group. In the
291 event of any discrepancy between this version and the original IEEE and
292 The Open Group Standard, the original IEEE and The Open Group Standard
293 is the referee document. The original Standard can be obtained online
294 at http://www.opengroup.org/unix/online.html .
295
296
297
298IEEE/The Open Group 2003 EXPR(1P)