summaryrefslogtreecommitdiff
path: root/corebinutils/expr/expr.1
blob: bd186059a725614bef815cc32fb5f10f91b502eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
.\" SPDX-License-Identifier: Apache-2.0
.\"
.\" Copyright (c) 2026 Project Tick
.\"
.\" Licensed under the Apache License, Version 2.0 (the "License");
.\" you may not use this file except in compliance with the License.
.\" You may obtain a copy of the License at
.\"
.\"    http://www.apache.org/licenses/LICENSE-2.0
.\"
.\" Unless required by applicable law or agreed to in writing, software
.\" distributed under the License is distributed on an "AS IS" BASIS,
.\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.\" See the License for the specific language governing permissions and
.\" limitations under the License.
.\"
.\" Copyright (c) 2026
.\" Project Tick
.Dd February 28, 2026
.Dt EXPR 1
.Os Linux
.Sh NAME
.Nm expr
.Nd evaluate integer, string, and regular-expression expressions
.Sh SYNOPSIS
.Nm
.Op Fl e
.Op Fl -
.Ar expression ...
.Sh DESCRIPTION
.Nm
evaluates the operand vector given on the command line and writes the result
to standard output.
Each operator and operand must be passed as a separate shell argument.
.Pp
This Linux port is a standalone implementation derived from FreeBSD
semantics, but it does not depend on the FreeBSD build system or
compatibility probes.
.Sh OPTIONS
.Bl -tag -width "-e"
.It Fl e
Enable compatibility parsing for numeric operands.
In this mode,
.Nm
accepts leading white space, an optional leading plus sign, and an empty
string in numeric context.
.It Fl -
End option processing.
Use
.Fl -
when the first operand may begin with
.Ql - .
.El
.Sh OPERANDS
Operators are evaluated from low precedence to high precedence in the
following order.
Operators with the same precedence associate left to right.
.Bl -tag -width "expr1 <= expr2"
.It Ar expr1 Li \&| Ar expr2
Return
.Ar expr1
if it is neither the empty string nor numeric zero.
Otherwise return
.Ar expr2
if it is non-empty and non-zero.
If both sides are null or zero, return
.Li 0 .
.It Ar expr1 Li & Ar expr2
Return
.Ar expr1
if both operands are non-empty and non-zero.
Otherwise return
.Li 0 .
.It Ar expr1 Li = Ar expr2
.It Ar expr1 Li != Ar expr2
.It Ar expr1 Li \&< Ar expr2
.It Ar expr1 Li \&<= Ar expr2
.It Ar expr1 Li \&> Ar expr2
.It Ar expr1 Li \&>= Ar expr2
If both operands are valid integers, compare them numerically.
Otherwise compare them as strings using the current locale collation rules.
The result is
.Li 1
for true and
.Li 0
for false.
.It Ar expr1 Li + Ar expr2
.It Ar expr1 Li - Ar expr2
.It Ar expr1 Li * Ar expr2
.It Ar expr1 Li / Ar expr2
.It Ar expr1 Li % Ar expr2
Perform checked arithmetic on
.Vt intmax_t .
Division and remainder by zero are rejected.
Overflow terminates the utility with an error.
.It Ar expr1 Li : Ar expr2
Match
.Ar expr1
against the basic regular expression
.Ar expr2 .
The match is anchored at the start of the string.
If the pattern contains a capturing subexpression and the match succeeds,
the first captured substring is returned.
If the pattern has no capturing subexpression and the match succeeds,
the length of the match is returned.
On a failed match, the result is the empty string when the pattern has a
capturing subexpression, otherwise
.Li 0 .
.El
.Pp
Parentheses may be used for grouping.
Because
.Nm
does not lexically separate operators from operands, a token that is
identical to an operator must be disambiguated by shell quoting or by
rewriting the expression.
.Sh SEMANTICS
By default, integer operands must be base-10 strings consisting of an
optional leading minus sign followed by digits.
In compatibility mode,
.Fl e ,
leading white space and a leading plus sign are accepted, and the empty
string is treated as zero in numeric context.
.Pp
This Linux port supports
.Fl -
for POSIX-style end-of-options handling.
It does not implement FreeBSD
.Xr check_utility_compat 3
probing or the historical
.Ev EXPR_COMPAT
environment switch.
.Sh EXIT STATUS
.Bl -tag -width indent -compact
.It 0
The final value is neither empty nor zero.
.It 1
The final value is the empty string or numeric zero.
.It 2
The expression is invalid or evaluation failed.
.El
.Sh DIAGNOSTICS
Errors are reported on standard error.
The implementation emits explicit diagnostics for syntax errors, invalid
decimal operands, arithmetic overflow, division by zero, regular-expression
compilation failures, and write failures.
.Sh EXAMPLES
.Bl -bullet
.It
Increment a shell variable:
.Dl n=$(expr "$n" + 1)
.It
Safely handle a leading negative operand:
.Dl n=$(expr -- "$n" + 1)
.It
Return the basename component of a path:
.Dl expr "//$path" : '.*/\e(.*\e)'
.It
Return the length of a shell variable:
.Dl expr \&( "X$text" : '.*' \&) - 1
.El
.Sh SEE ALSO
.Xr sh 1 ,
.Xr test 1 ,
.Xr regex 3
.Sh STANDARDS
The strict parsing mode follows the POSIX utility syntax model for option
handling and preserves the historical
.Nm
operator set.
Compatibility mode
.Pq Fl e
is a local extension for FreeBSD-script migration.