date and time created 88/10/18 21:50:57 by bostic
[unix-history] / usr / src / bin / expr / expr.1
CommitLineData
0e1c2e9d 1.\" @(#)expr.1 6.1 (Berkeley) %G%
b9c0268e 2.\"
0e1c2e9d 3.TH EXPR 1 ""
b9c0268e
KM
4.AT 3
5.SH NAME
6expr \- evaluate arguments as an expression
7.SH SYNOPSIS
8.B expr
9arg
10.B .\|.\|.
11.SH DESCRIPTION
12The arguments are taken as an expression.
13After evaluation, the result is written on the standard output.
14Each token of the expression is a separate argument.
15.PP
16The operators and keywords are listed below.
17The list is in order of increasing precedence,
18with equal precedence operators grouped.
19.TP
20.I expr | expr
21yields the first
22.I expr
23if it is neither null nor `0', otherwise
24yields the second
25.I expr.
26.TP
27.I expr & expr
28yields the first
29.I expr
30if neither
31.I expr
32is null or `0', otherwise yields `0'.
33.TP
34.I expr relop expr
35where
36.I relop is one of
37< <= = != >= >,
38yields `1' if the indicated comparison is true, `0' if false.
39The comparison is numeric if both
40.I expr
41are integers, otherwise lexicographic.
42.TP
43.IR expr " + " expr
44.br
45.ns
46.TP
47.IR expr " \- " expr
48.br
49addition or subtraction of the arguments.
50.TP
51.IR expr " * " expr
52.br
53.ns
54.TP
55.IR expr " / " expr
56.br
57.ns
58.TP
59.IR expr " % " expr
60.br
61multiplication, division, or remainder of the arguments.
62.TP
63.IR expr " : " expr
64The matching operator compares the string first argument
65with the regular expression second argument;
66regular expression syntax is the same as that of
67.IR ed (1).
68The
69\fB\\(\|.\|.\|.\|\\)\fP
70pattern symbols can be used to select a portion of the
71first argument.
72Otherwise,
73the matching operator yields the number of characters matched
74(`0' on failure).
75.TP
76.RI ( " expr " )
77parentheses for grouping.
78.PP
79Examples:
80.PP
81To add 1 to the Shell variable
82.IR a :
83.IP
84a=\`expr $a + 1\`
85.PP
86To find the filename part (least significant part)
87of the pathname stored in variable
88.I a,
89which may or may not contain `/':
90.IP
91expr $a : \'.*/\e(\^.*\e)\' \'\^|\' $a
92.LP
93Note the quoted Shell metacharacters.
94.SH "SEE ALSO"
95sh(1), test(1)
96.SH DIAGNOSTICS
97.I Expr
98returns the following exit codes:
99.PP
100 0 if the expression is neither null nor `0',
101.br
102 1 if the expression
103is null or `0',
104.br
105 2 for invalid expressions.