1EXPR(P)                    POSIX Programmer's Manual                   EXPR(P)
2
3
4

NAME

6       expr - evaluate arguments as an expression
7

SYNOPSIS

9       expr operand
10

DESCRIPTION

12       The  expr  utility shall evaluate an expression and write the result to
13       standard output.
14

OPTIONS

16       None.
17

OPERANDS

19       The single expression evaluated by expr shall be formed from the  oper‐
20       ands, as described in the EXTENDED DESCRIPTION section. The application
21       shall ensure that each of the expression operator symbols:
22
23
24              (  )  |  &  =  >  >=  <  <=  !=  +  -  *  /  %  :
25
26       and the symbols integer and string in the table are provided  as  sepa‐
27       rate arguments to expr.
28

STDIN

30       Not used.
31

INPUT FILES

33       None.
34

ENVIRONMENT VARIABLES

36       The following environment variables shall affect the execution of expr:
37
38       LANG   Provide  a  default value for the internationalization variables
39              that are unset or null. (See  the  Base  Definitions  volume  of
40              IEEE Std 1003.1-2001,  Section  8.2,  Internationalization Vari‐
41              ables for the precedence of internationalization variables  used
42              to determine the values of locale categories.)
43
44       LC_ALL If  set  to a non-empty string value, override the values of all
45              the other internationalization variables.
46
47       LC_COLLATE
48
49              Determine the locale for the  behavior  of  ranges,  equivalence
50              classes,  and  multi-character collating elements within regular
51              expressions and by the string comparison operators.
52
53       LC_CTYPE
54              Determine the locale for  the  interpretation  of  sequences  of
55              bytes  of  text  data as characters (for example, single-byte as
56              opposed to multi-byte characters in arguments) and the  behavior
57              of character classes within regular expressions.
58
59       LC_MESSAGES
60              Determine  the  locale  that should be used to affect the format
61              and contents of diagnostic messages written to standard error.
62
63       NLSPATH
64              Determine the location of message catalogs for the processing of
65              LC_MESSAGES .
66
67

ASYNCHRONOUS EVENTS

69       Default.
70

STDOUT

72       The  expr  utility  shall evaluate the expression and write the result,
73       followed by a <newline>, to standard output.
74

STDERR

76       The standard error shall be used only for diagnostic messages.
77

OUTPUT FILES

79       None.
80

EXTENDED DESCRIPTION

82       The formation of the expression to be evaluated is shown in the follow‐
83       ing  table.  The  symbols  expr, expr1, and expr2 represent expressions
84       formed from integer and string symbols and the expression operator sym‐
85       bols  (all  separate  arguments)  by  recursive application of the con‐
86       structs described in the table. The expressions are listed in order  of
87       increasing  precedence, with equal-precedence operators grouped between
88       horizontal lines. All of the operators shall be left-associative.
89
90              Expression       Description
91              expr1 | expr2    Returns the evaluation of expr1 if it is
92                               neither null nor zero; otherwise,
93                               returns the evaluation of expr2 if it is
94                               not null; otherwise, zero.
95              expr1 & expr2    Returns the evaluation of expr1 if nei‐
96                               ther expression evaluates to null or
97                               zero; otherwise, returns zero.
98                               Returns the result of a decimal integer
99                               comparison if both arguments are inte‐
100                               gers; otherwise, returns the result of a
101                               string comparison using the locale-spe‐
102                               cific collation sequence. The result of
103                               each comparison is 1 if the specified
104                               relationship is true, or 0 if the rela‐
105                               tionship is false.
106              expr1 = expr2    Equal.
107              expr1 > expr2    Greater than.
108              expr1 >= expr2   Greater than or equal.
109              expr1 < expr2    Less than.
110              expr1 <= expr2   Less than or equal.
111              expr1 != expr2   Not equal.
112              expr1 + expr2    Addition of decimal integer-valued argu‐
113                               ments.
114              expr1 - expr2    Subtraction of decimal integer-valued
115                               arguments.
116              expr1 * expr2    Multiplication of decimal integer-valued
117                               arguments.
118              expr1 / expr2    Integer division of decimal integer-val‐
119                               ued arguments, producing an integer
120                               result.
121              expr1 % expr2    Remainder of integer division of decimal
122                               integer-valued arguments.
123              expr1 : expr2    Matching expression; see below.
124              ( expr )         Grouping symbols. Any expression can be
125                               placed within parentheses. Parentheses
126                               can be nested to a depth of
127                               {EXPR_NEST_MAX}.
128              integer          An argument consisting only of an
129                               (optional) unary minus followed by dig‐
130                               its.
131              string           A string argument; see below.
132
133   Matching Expression
134       The  ':'  matching operator shall compare the string resulting from the
135       evaluation of expr1 with the regular expression pattern resulting  from
136       the  evaluation  of  expr2.  Regular  expression  syntax  shall be that
137       defined in the Base Definitions volume of IEEE Std 1003.1-2001, Section
138       9.3,  Basic  Regular Expressions, except that all patterns are anchored
139       to the beginning of the string (that is, only sequences starting at the
140       first character of a string are matched by the regular expression) and,
141       therefore, it is unspecified whether '^' is a special character in that
142       context.  Usually,  the  matching operator shall return a string repre‐
143       senting the number of characters matched ( '0'  on  failure).  Alterna‐
144       tively,  if the pattern contains at least one regular expression subex‐
145       pression "[\(...\)]" ,  the  string  corresponding  to  "\1"  shall  be
146       returned.
147
148   String Operand
149       A  string argument is an argument that cannot be identified as an inte‐
150       ger argument or as one of the expression operator symbols shown in  the
151       OPERANDS section.
152
153       The  use  of  string arguments length, substr, index, or match produces
154       unspecified results.
155

EXIT STATUS

157       The following exit values shall be returned:
158
159        0     The expression evaluates to neither null nor zero.
160
161        1     The expression evaluates to null or zero.
162
163        2     Invalid expression.
164
165       >2     An error occurred.
166
167

CONSEQUENCES OF ERRORS

169       Default.
170
171       The following sections are informative.
172

APPLICATION USAGE

174       After argument processing by the shell, expr is not required to be able
175       to tell the difference between an operator and an operand except by the
176       value. If "$a" is '=' , the command:
177
178
179              expr $a = '='
180
181       looks like:
182
183
184              expr = = =
185
186       as the arguments are passed to expr (and they all may be taken  as  the
187       '=' operator). The following works reliably:
188
189
190              expr X$a = X=
191
192       Also  note that this volume of IEEE Std 1003.1-2001 permits implementa‐
193       tions to extend utilities. The expr utility permits the  integer  argu‐
194       ments  to  be  preceded  with a unary minus. This means that an integer
195       argument could look like an option. Therefore, the conforming  applica‐
196       tion must employ the "--" construct of Guideline 10 of the Base Defini‐
197       tions volume of  IEEE Std 1003.1-2001,  Section  12.2,  Utility  Syntax
198       Guidelines to protect its operands if there is any chance the first op‐
199       erand might be a negative integer (or any string with a leading minus).
200

EXAMPLES

202       The expr utility has a rather difficult syntax:
203
204        * Many of the operators are also shell control operators  or  reserved
205          words, so they have to be escaped on the command line.
206
207        * Each  part  of  the expression is composed of separate arguments, so
208          liberal usage of <blank>s is required. For example:
209
210                        Invalid            Valid
211                        expr 1+2           expr 1 + 2
212                        expr "1 + 2"       expr 1 + 2
213                        expr 1 + (2 * 3)   expr 1 + \( 2 \* 3 \)
214
215       In many cases, the arithmetic and string features provided as  part  of
216       the  shell command language are easier to use than their equivalents in
217       expr. Newly written scripts should avoid expr in favor of the new  fea‐
218       tures  within  the  shell;  see Parameters and Variables and Arithmetic
219       Expansion .
220
221       The following command:
222
223
224              a=$(expr $a + 1)
225
226       adds 1 to the variable a.
227
228       The following command, for "$a" equal to either /usr/abc/file  or  just
229       file:
230
231
232              expr $a : '.*/\(.*\)' \| $a
233
234       returns  the  last  segment of a pathname (that is, file). Applications
235       should avoid the character '/' used alone  as  an  argument;  expr  may
236       interpret it as the division operator.
237
238       The following command:
239
240
241              expr "//$a" : '.*/\(.*\)'
242
243       is a better representation of the previous example. The addition of the
244       "//" characters eliminates any ambiguity about  the  division  operator
245       and  simplifies the whole expression. Also note that pathnames may con‐
246       tain characters contained in the IFS variable and should be  quoted  to
247       avoid having "$a" expand into multiple arguments.
248
249       The following command:
250
251
252              expr "$VAR" : '.*'
253
254       returns the number of characters in VAR.
255

RATIONALE

257       In an early proposal, EREs were used in the matching expression syntax.
258       This was changed to BREs to avoid breaking historical applications.
259
260       The use of a leading circumflex in the BRE is unspecified because  many
261       historical  implementations  have  treated  it  as a special character,
262       despite their system documentation. For example:
263
264
265              expr foo : ^foo     expr ^foo : ^foo
266
267       return 3 and 0, respectively, on  those  systems;  their  documentation
268       would imply the reverse. Thus, the anchoring condition is left unspeci‐
269       fied to avoid breaking historical scripts relying on this  undocumented
270       feature.
271

FUTURE DIRECTIONS

273       None.
274

SEE ALSO

276       Parameters and Variables , Arithmetic Expansion
277
279       Portions  of  this text are reprinted and reproduced in electronic form
280       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
281       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
282       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
283       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
284       event of any discrepancy between this version and the original IEEE and
285       The  Open Group Standard, the original IEEE and The Open Group Standard
286       is the referee document. The original Standard can be  obtained  online
287       at http://www.opengroup.org/unix/online.html .
288
289
290
291IEEE/The Open Group                  2003                              EXPR(P)
Impressum