1.\" Copyright (c) 1993 Winning Strategies, Inc. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by Winning Strategies, Inc. 15.\" 4. The name of the author may not be used to endorse or promote products 16.\" derived from this software withough specific prior written permission 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.\" $Header: /cvsroot/src/bin/expr/expr.1,v 1.1 1993/07/20 20:16:26 jtc Exp $ 30.\" 31.Dd July 3, 1993 32.Dt EXPR 1 33.Os 34.Sh NAME 35.Nm expr 36.Nd evaluate expression 37.Sh SYNOPSIS 38.Nm expr 39.Ar expression 40.Sh DESCRIPTION 41The 42.Nm expr 43utility evaluates 44.Ar expression 45and writes the result on standard output. 46.Pp 47All operators are separate arguments to the 48.Nm expr 49utility. 50Characters special to the command interpreter must be escaped. 51.Pp 52Operators are listed below in order of increasing precidence. 53Operators with equal precidence are grouped within { } symbols. 54.Bl -tag -width indent 55.It Ar expr1 Li | Ar expr2 56Returns the evaluation of 57.Ar expr1 58if it is neither an empty string nor zero; 59otherwise, returns the evaluation of 60.Ar expr2 . 61.It Ar expr1 Li & Ar expr2 62Returns the evaluation of 63.Ar expr1 64if neither expression evaluates to an empty string or zero; 65otherwise, returns zero. 66.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 67Returns the results of integer comparision if both arguments are integers; 68otherwise, returns the results of lexical comparison. 69The result of each comparison is 1 if the specified relation is true, 70or 0 if the relation is false. 71.It Ar expr1 Li "{+, -}" Ar expr2 72Returns the results of addition or subtraction of integer-valued arguments. 73.It Ar expr1 Li "{*, /, %}" Ar expr2 74Returns the results of multiplication, integer division, or remainder of integer-valued arguments. 75.It Ar expr1 Li : Ar expr2 76The 77.Dq \: 78operator matches 79.Ar expr1 80against 81.Ar expr2 , 82which must be a regular expression. The regular expression is anchored 83to the begining of the string with an invisible 84.Dq ^ , 85therefore 86.Dq ^ 87is not a special character. 88.Pp 89If the pattern contains at lest one regular expression 90subexpression 91.Dq "\e(...\e)" , 92the string corresponding to 93.Dq "\e1" 94is returned; 95otherwise the matching operator returns the number of characters matched. 96.El 97.Pp 98Parentheses are used for grouping in the usual manner. 99.Sh EXAMPLES 100.Bl -enum 101.It 102The following example adds one to the variable a. 103.Dl a=`expr $a + 1` 104.It 105The following example returns the filename portion of a pathname stored 106in variable a. The // characters act to eliminate ambiguity with the 107division operator. 108.Dl expr "//$a" Li : '.*/\e(.*\e)' 109.It 110The following example returns the number of characters in variable a. 111.Dl expr $a Li : '.*' 112.El 113.Sh DIAGNOSTICS 114The 115.Nm expr 116utility exits with one of the following values: 117.Bl -tag -width Ds 118.It 0 119the expression is neither an empty string nor 0. 120.It 1 121the expression is an empty string or 0. 122.It 2 123the expression is invalid. 124.El 125.Sh STANDARDS 126The 127.Nm expr 128utility 129is expected to be 130.St -p1003.2 131compatible. 132