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