Commit | Line | Data |
---|---|---|
ee7d659d WJ |
1 | |
2 | ||
3 | ||
4 | EXPR(1L) EXPR(1L) | |
5 | ||
6 | ||
7 | N\bNA\bAM\bME\bE | |
8 | expr - evaluate expressions | |
9 | ||
10 | S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS | |
11 | e\bex\bxp\bpr\br expression... | |
12 | ||
13 | D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN | |
14 | This manual page documents the GNU version of e\bex\bxp\bpr\br. e\bex\bxp\bpr\br | |
15 | evaluates an expression and writes the result on its stan- | |
16 | dard output. Each token of the expression must be a sepa- | |
17 | rate argument. Operands are either numbers or strings. | |
18 | Strings are not quoted for e\bex\bxp\bpr\br, though you may need to | |
19 | quote them to protect them from the shell. e\bex\bxp\bpr\br coerces | |
20 | anything appearing in an operand position to an integer or | |
21 | a string depending on the operation being applied to it. | |
22 | ||
23 | The operators (in order of increasing precedence) are: | |
24 | ||
25 | | yields its first argument if it is neither null nor | |
26 | 0, otherwise its second argument. This is the | |
27 | usual `or' operation. | |
28 | ||
29 | & yields its first argument if neither argument is | |
30 | null or 0, otherwise 0. | |
31 | ||
32 | < <= = != >= > | |
33 | compare their arguments and return `1' if the rela- | |
34 | tion is true, 0 otherwise. e\bex\bxp\bpr\br tries to coerce | |
35 | both arguments to numbers and do a numeric compari- | |
36 | son; if it fails when trying to coerce either argu- | |
37 | ment it then does a lexicographic comparison. | |
38 | ||
39 | + - perform arithmetic operations. Both arguments are | |
40 | coerced to numbers; an error occurs if this cannot | |
41 | be done. | |
42 | ||
43 | * / % perform arithmetic operations (`%' is the remainder | |
44 | operation, as in C). Both arguments are coerced to | |
45 | numbers; an error occurs if this cannot be done. | |
46 | ||
47 | : performs pattern matching. Its arguments are | |
48 | coerced to strings and the second one is considered | |
49 | to be a regular expression, with a `^' implicitly | |
50 | added at the beginning. The first argument is then | |
51 | matched against this regular expression. If the | |
52 | match succeeds and part of the string is enclosed | |
53 | in `\(' and `\)', that part is the value of the : | |
54 | expression; otherwise an integer whose value is the | |
55 | number of characters matched is returned. If the | |
56 | match fails, the : operator returns the null string | |
57 | if `\(' and `\)' are used, otherwise 0. Only one | |
58 | `\(' and `\)' pair can be used. | |
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | 1 | |
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | EXPR(1L) EXPR(1L) | |
71 | ||
72 | ||
73 | Parentheses are used for grouping in the usual manner. | |
74 | ||
75 | Examples: | |
76 | ||
77 | To add 1 to the shell variable _\ba: | |
78 | ||
79 | a=`expr $a + 1` | |
80 | ||
81 | To find the filename part of the pathname stored in vari- | |
82 | able _\ba, which may or may not contain `/': | |
83 | ||
84 | expr $a : '.*/\(.*\)' '|' $a | |
85 | ||
86 | Note the quoted shell metacharacters. | |
87 | ||
88 | e\bex\bxp\bpr\br returns the following exit status: | |
89 | ||
90 | 0 if the expression is neither null nor 0, | |
91 | 1 if the expression is null or 0, | |
92 | 2 for invalid expressions. | |
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 | 2 | |
131 | ||
132 |