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