1expr(1B)           SunOS/BSD Compatibility Package Commands           expr(1B)
2
3
4

NAME

6       expr  -  evaluate arguments as a logical, arithmetic, or string expres‐
7       sion
8

SYNOPSIS

10       /usr/ucb/expr argument...
11
12

DESCRIPTION

14       The expr utility evaluates expressions as specified by  its  arguments.
15       After  evaluation,  the  result is written on the standard output. Each
16       token of the expression is a separate argument, so terms of the expres‐
17       sion  must be separated by blanks. Characters special to the shell must
18       be escaped. Note: 0 is returned to indicate a zero value,  rather  than
19       the  null string. Strings containing blanks or other special characters
20       should be quoted. Integer-valued arguments may be preceded by  a  unary
21       minus  sign.  Internally, integers are treated as 32-bit, two's-comple‐
22       ment numbers.
23
24
25       The operators and keywords are listed below. Characters that need to be
26       escaped  are preceded by `\'. The list is in order of increasing prece‐
27       dence, with equal precedence operators grouped within {} symbols.
28
29       expr \| expr
30
31           Returns the evaluation of the first expr if it is neither NULL  nor
32           0;  otherwise,  returns  the evaluation of the second expr if it is
33           not NULL; otherwise, 0.
34
35
36       expr \& expr
37
38           Returns the first expr if neither expr  is  NULL  or  0,  otherwise
39           returns 0.
40
41
42       expr { =, \, \ , \<, \<=, != } expr
43
44           Returns  the  result of an integer comparison if both arguments are
45           integers, otherwise returns the result of a lexical comparison.
46
47
48       expr { +, } expr
49
50           Addition or subtraction of integer-valued arguments.
51
52
53       expr { \, /, % } expr
54
55           Multiplication, division, or remainder of the integer-valued  argu‐
56           ments.
57
58
59       string : regular-expression
60       match string regular-expression
61
62           The  two  forms  of the matching operator above are synonymous. The
63           matching operators : and match compare the first argument with  the
64           second argument which must be a regular expression. Regular expres‐
65           sion syntax is the same as that of regexp(5), except that all  pat‐
66           terns  are  "anchored" (treated as if they begin with ^) and there‐
67           fore ^ is not a special character, in that context.  Normally,  the
68           matching  operator  returns  the number of characters matched (0 on
69           failure). Alternatively, the \...\ pattern symbols can be  used  to
70           return a portion of the first argument.
71
72
73       substr string integer-1 integer-2
74
75           Extracts  the  substring  of  string starting at position integer-1
76           and of length  integer-2  characters.  If  integer-1  has  a  value
77           greater  than  the length of string, expr returns a null string. If
78           you try to extract more characters than there are in  string,  expr
79           returns  all  the remaining characters from string. Beware of using
80           negative values for either integer-1 or integer-2 as  expr tends to
81           run forever in these cases.
82
83
84       index string character-list
85
86           Reports  the first position in string at which any one of the char‐
87           acters in character-list matches a character in  string.
88
89
90       length string
91
92           Returns the length (that is, the number of characters) of string.
93
94
95       ( expr )
96
97           Parentheses may be used for grouping.
98
99

EXAMPLES

101       Example 1 Adding an integer to a shell variable
102
103
104       Add 1 to the shell variable a.
105
106
107         a='expr $a + 1'
108
109
110
111       Example 2 Returning a path name segment
112
113
114       Return the last segment of a path name (that is,  the  filename  part).
115       Watch out for / alone as an argument: expr will take it as the division
116       operator (see BUGS below).
117
118
119         # 'For $a equal to either "/usr/abc/file" or just "file"'
120         expr  $a  :  '.*/\  \  $a
121
122
123
124       Example 3 Using // characters to simplify the expression
125
126
127       The addition of the // characters eliminates any  ambiguity  about  the
128       division operator and simplifies the whole expression.
129
130
131         # A better representation of example 2.
132         expr  //$a  :  '.*/\
133
134
135
136       Example 4 Returning the value of a variable
137
138
139       Returns the number of characters in $VAR.
140
141
142         expr  $VAR  :  '.*'
143
144
145

EXIT STATUS

147       expr returns the following exit codes:
148
149       0    If the expression is neither  NULL nor 0.
150
151
152       1    If the expression is NULL or 0.
153
154
155       2    For invalid expressions.
156
157

ATTRIBUTES

159       See attributes(5) for descriptions of the following attributes:
160
161
162
163
164       ┌─────────────────────────────┬─────────────────────────────┐
165       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
166       ├─────────────────────────────┼─────────────────────────────┤
167       │Availability                 │SUNWscpu                     │
168       └─────────────────────────────┴─────────────────────────────┘
169

SEE ALSO

171       sh(1), test(1), attributes(5), regexp(5)
172

DIAGNOSTICS

174       syntax error            for operator/operand errors
175
176
177       non-numeric argument    if arithmetic is attempted on such a string
178
179
180       division by zero        if an attempt to divide by zero is made
181
182

BUGS

184       After argument processing by the shell, expr cannot tell the difference
185       between an operator and an operand except by the value. If $a is an  =,
186       the command:
187
188         expr  $a  =  '='
189
190
191
192
193       looks like:
194
195         expr  =  =  =
196
197
198
199
200       as  the arguments are passed to expr (and they will all be taken as the
201       = operator). The following works:
202
203         expr  X$a  =  X=
204
205
206
207
208       Note: the match, substr, length, and index operators cannot  themselves
209       be used as ordinary strings.  That is, the expression:
210
211         example% expr index expurgatorious length
212         syntax error
213         example%
214
215
216
217
218       generates the `syntax error' message as shown instead of the value 1 as
219       you might expect.
220
221
222
223SunOS 5.11                        6 Jun 2000                          expr(1B)
Impressum