BSD 3 development
[unix-history] / usr / man / man1 / expr.1
CommitLineData
e6817382
BJ
1.TH EXPR 1
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.IR expr " - " expr
43.br
44addition or subtraction of the arguments.
45.TP
46.IR expr " * " expr
47.br
48.IR expr " / " expr
49.br
50.IR expr " % " expr
51.br
52multiplication, division, or remainder of the arguments.
53.TP
54.IR expr " : " expr
55The matching operator compares the string first argument
56with the regular expression second argument;
57regular expression syntax is the same as that of
58.IR ed (1).
59The
60\fB\\(\|.\|.\|.\|\\)\fP
61pattern symbols can be used to select a portion of the
62first argument.
63Otherwise,
64the matching operator yields the number of characters matched
65(`0' on failure).
66.TP
67.RI ( " expr " )
68parentheses for grouping.
69.PP
70Examples:
71.PP
72To add 1 to the Shell variable
73.IR a :
74.IP
75a=\`expr $a + 1\`
76.PP
77To find the filename part (least significant part)
78of the pathname stored in variable
79.I a,
80which may or may not contain `/':
81.IP
82expr $a : \'.*/\e(\^.*\e)\' \'\^|\' $a
83.LP
84Note the quoted Shell metacharacters.
85.SH "SEE ALSO"
86ed(1), sh(1), test(1)
87.SH DIAGNOSTICS
88.I Expr
89returns the following exit codes:
90.PP
91 0 if the expression is neither null nor `0',
92.br
93 1 if the expression
94is null or `0',
95.br
96 2 for invalid expressions.