xref: /openbsd-src/bin/expr/expr.1 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: expr.1,v 1.10 2000/10/18 06:55:08 aaron Exp $
2.\"	$NetBSD: expr.1,v 1.9 1995/04/28 23:27:13 jtc Exp $
3.\"
4.\" Written by J.T. Conklin <jtc@netbsd.org>.
5.\" Public domain.
6.\"
7.Dd July 3, 1993
8.Dt EXPR 1
9.Os
10.Sh NAME
11.Nm expr
12.Nd evaluate expression
13.Sh SYNOPSIS
14.Nm expr
15.Ar expression
16.Sh DESCRIPTION
17The
18.Nm
19utility evaluates
20.Ar expression
21and writes the result on standard output.
22All operators are separate arguments to the
23.Nm
24utility.
25Characters special to the command interpreter must be escaped.
26.Pp
27Operators are listed below in order of increasing precedence.
28Operators with equal precedence are grouped within { } symbols.
29.Bl -tag -width indent
30.It Ar expr1 Li | Ar expr2
31Returns the evaluation of
32.Ar expr1
33if it is neither an empty string nor zero;
34otherwise, returns the evaluation of
35.Ar expr2 .
36.It Ar expr1 Li & Ar expr2
37Returns the evaluation of
38.Ar expr1
39if neither expression evaluates to an empty string or zero;
40otherwise, returns zero.
41.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
42Returns the results of integer comparison if both arguments are integers;
43otherwise, returns the results of string comparison using the locale-specific
44collation sequence.
45The result of each comparison is 1 if the specified relation is true,
46or 0 if the relation is false.
47.It Ar expr1 Li "{+, -}" Ar expr2
48Returns the results of addition or subtraction of integer-valued arguments.
49.It Ar expr1 Li "{*, /, %}" Ar expr2
50Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
51.It Ar expr1 Li : Ar expr2
52The
53.Ql \&:
54operator matches
55.Ar expr1
56against
57.Ar expr2 ,
58which must be a regular expression.
59The regular expression is anchored
60to the beginning of the string with an implicit
61.Ql ^ .
62.Pp
63If the match succeeds and the pattern contains at least one regular
64expression subexpression
65.Dq "\e(...\e)" ,
66the string corresponding to
67.Dq "\e1"
68is returned;
69otherwise, the matching operator returns the number of characters matched.
70If the match fails and the pattern contains a regular expression subexpression
71the null string is returned;
72otherwise, returns 0.
73.El
74.Pp
75Parentheses are used for grouping in the usual manner.
76.Sh EXAMPLES
77.Cm a=`expr $a + 1`
78.Pp
79Add 1 to the variable
80.Va a .
81.Pp
82.Cm expr "//$a" \&: '.*/\e(.*\e)'
83.Pp
84Return the filename portion of a pathname stored
85in variable
86.Va a .
87The
88.Ql //
89characters act to eliminate ambiguity with the division operator.
90.Pp
91.Cm expr $a \&: '.*'
92.Pp
93Return the number of characters in variable
94.Va a .
95.Sh DIAGNOSTICS
96The
97.Nm
98utility exits with one of the following values:
99.Pp
100.Bl -tag -width Ds -compact
101.It 0
102The expression is neither an empty string nor 0.
103.It 1
104The expression is an empty string or 0.
105.It 2
106The expression is invalid.
107.It >2
108An error occurred (such as memory allocation failure).
109.El
110.Sh SEE ALSO
111.Xr test 1
112.Sh STANDARDS
113The
114.Nm
115utility conforms to
116.St -p1003.2 .
117