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