1expr(1)                          User Commands                         expr(1)
2
3
4

NAME

6       expr - evaluate arguments as an expression
7

SYNOPSIS

9       /usr/bin/expr argument...
10
11
12       /usr/xpg4/bin/expr argument...
13
14
15       /usr/xpg6/bin/expr argument...
16
17

DESCRIPTION

19   /usr/bin/expr, /usr/xpg4/bin/expr
20       The  expr  utility  evaluates  the  expression and writes the result to
21       standard output. The character 0 is written to indicate  a  zero  value
22       and nothing is written to indicate a null string.
23
24   /usr/xpg6/bin/expr
25       The  expr  utility  evaluates  the  expression and writes the result to
26       standard output followed by a NEWLINE. If there is no result from  expr
27       processing, a NEWLINE is written to standard output.
28

OPERANDS

30       The  argument  operand  is  evaluated  as  an  expression. Terms of the
31       expression must be separated by blanks. Characters special to the shell
32       must be escaped (see sh(1)). Strings containing blanks or other special
33       characters should be quoted. The length of the expression is limited to
34       LINE_MAX (2048 characters).
35
36
37       The  operators  and  keywords are listed below. The list is in order of
38       increasing precedence, with equal precedence operators  grouped  within
39       {} symbols. All of the operators are left-associative.
40
41       expr \| expr
42
43           Returns  the evaluation of the first expr if it is neither NULL nor
44           0; otherwise, returns the evaluation of the second expr  if  it  is
45           not NULL; otherwise, 0.
46
47
48       expr \& expr
49
50           Returns  the  first  expr  if  neither expr is NULL or 0, otherwise
51           returns 0.
52
53
54       expr{ =, \>, \>=, \<, \<=, !=} expr
55
56           Returns the result of an integer comparison if both  arguments  are
57           integers, otherwise returns the result of a string comparison using
58           the locale-specific coalition sequence. The result of each compari‐
59           son will be 1 if the specified relationship is TRUE, 0 if the rela‐
60           tionship is FALSE.
61
62
63       expr { +, } expr
64
65           Addition or subtraction of integer-valued arguments.
66
67
68       expr { \*, /, %} expr
69
70           Multiplication, division, or remainder of the integer-valued  argu‐
71           ments.
72
73
74       expr : expr
75
76           The  matching  operator  : (colon) compares the first argument with
77           the second argument, which must be an internationalized basic regu‐
78           lar  expression (BRE), except that all patterns are anchored to the
79           beginning of the string. That is, only sequences  starting  at  the
80           first  character of a string are matched by the regular expression.
81           See regex(5) and NOTES. Normally, the /usr/bin/expr matching opera‐
82           tor  returns the number of bytes matched and the /usr/xpg4/bin/expr
83           matching operator returns the number of characters  matched  (0  on
84           failure).  If  the  second  argument contains at least one BRE sub-
85           expression [\(...\)], the matching operator returns the string cor‐
86           responding to \1.
87
88
89       integer
90
91           An  argument  consisting only of an (optional) unary minus followed
92           by digits.
93
94
95       string
96
97           A string argument that cannot be identified as an integer  argument
98           or as one of the expression operator symbols.
99
100
101   Compatibility Operators (x86 only)
102       The following operators are included for compatibility with INTERACTIVE
103       UNIX System only and are not intended to be used  by  non-  INTERACTIVE
104       UNIX System scripts:
105
106       index string character-list
107
108           Report  the first position in which any one of the bytes in charac‐
109           ter-list matches a byte in string.
110
111
112       length string
113
114           Return the length (that is, the number of bytes) of string.
115
116
117       substr string integer-1 integer-2
118
119           Extract the substring of string starting at position integer-1  and
120           of  length  integer-2 bytes.  If integer-1 has a value greater than
121           the number of bytes in string, expr returns a null string.  If  you
122           try  to  extract  more bytes than there are in string, expr returns
123           all the remaining bytes from string.  Results  are  unspecified  if
124           either integer-1 or integer-2 is a negative value.
125
126

EXAMPLES

128       Example 1 Adding an integer to a shell variable
129
130
131       Add 1 to the shell variable a:
132
133
134         example$ a=`expr $a + 1`
135
136
137
138       Example 2 Returning a path name segment
139
140
141       The  following example emulates basename(1), returning the last segment
142       of the path name $a. For $a equal to either /usr/abc/file or just file,
143       the  example  returns file. (Watch out for / alone as an argument: expr
144       takes it as the division operator. See NOTES below.)
145
146
147         example$ expr $a : '.*/\(.*\)' \| $a
148
149
150
151       Example 3 Using // characters to simplify the expression
152
153
154       Here is a better version of the previous example. The addition  of  the
155       //  characters eliminates any ambiguity about the division operator and
156       simplifies the whole expression.
157
158
159         example$ expr //$a : '.*/\(.*\)'
160
161
162
163   /usr/bin/expr
164       Example 4 Returning the number of bytes in a variable
165
166         example$ expr "$VAR" : '.*'
167
168
169
170   /usr/xpg4/bin/expr
171       Example 5 Returning the number of characters in a variable
172
173         example$ expr "$VAR" : '.*'
174
175
176

ENVIRONMENT VARIABLES

178       See environ(5) for descriptions of the following environment  variables
179       that  affect the execution of expr: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
180       LC_MESSAGES, and NLSPATH.
181

EXIT STATUS

183       As a side effect of expression evaluation, expr returns  the  following
184       exit values:
185
186       0      If the expression is neither NULL nor 0.
187
188
189       1      If the expression is either NULL or 0.
190
191
192       2      For invalid expressions.
193
194
195       >2     An error occurred.
196
197

ATTRIBUTES

199       See attributes(5) for descriptions of the following attributes:
200
201
202
203
204       ┌─────────────────────────────┬─────────────────────────────┐
205       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
206       ├─────────────────────────────┼─────────────────────────────┤
207       │Availability                 │SUNWcsu                      │
208       ├─────────────────────────────┼─────────────────────────────┤
209       │CSI                          │enabled                      │
210       ├─────────────────────────────┼─────────────────────────────┤
211       │Interface Stability          │Standard                     │
212       └─────────────────────────────┴─────────────────────────────┘
213

SEE ALSO

215       basename(1),   ed(1),   sh(1),   Intro(3),  attributes(5),  environ(5),
216       regex(5), standards(5)
217

DIAGNOSTICS

219       syntax error            Operator and operand errors.
220
221
222       non-numeric argument    Arithmetic is attempted on such a string.
223
224

NOTES

226       After argument processing by the shell, expr cannot tell the difference
227       between  an operator and an operand except by the value. If $a is an =,
228       the command:
229
230         example$ expr $a = '='
231
232
233
234
235       looks like:
236
237         example$ expr = = =
238
239
240
241
242       as the arguments are passed to expr (and they are all taken  as  the  =
243       operator). The following works:
244
245         example$ expr X$a = X=
246
247
248
249   Regular Expressions
250       Unlike  some previous versions, expr uses Internationalized Basic Regu‐
251       lar Expressions for all system-provided locales. Internationalized Reg‐
252       ular Expressions are explained on the regex(5) manual page.
253
254
255
256SunOS 5.11                        29 Aug 2003                          expr(1)
Impressum