1d8c21a3eSJ.T. Conklin.\" -*- nroff -*- 2d8c21a3eSJ.T. Conklin.\" 3c9fe00dcSJ.T. Conklin.\" Copyright (c) 1993 Winning Strategies, Inc. 4c9fe00dcSJ.T. Conklin.\" All rights reserved. 5c9fe00dcSJ.T. Conklin.\" 6c9fe00dcSJ.T. Conklin.\" Redistribution and use in source and binary forms, with or without 7c9fe00dcSJ.T. Conklin.\" modification, are permitted provided that the following conditions 8c9fe00dcSJ.T. Conklin.\" are met: 9c9fe00dcSJ.T. Conklin.\" 1. Redistributions of source code must retain the above copyright 10c9fe00dcSJ.T. Conklin.\" notice, this list of conditions and the following disclaimer. 11c9fe00dcSJ.T. Conklin.\" 2. Redistributions in binary form must reproduce the above copyright 12c9fe00dcSJ.T. Conklin.\" notice, this list of conditions and the following disclaimer in the 13c9fe00dcSJ.T. Conklin.\" documentation and/or other materials provided with the distribution. 14c9fe00dcSJ.T. Conklin.\" 3. All advertising materials mentioning features or use of this software 15c9fe00dcSJ.T. Conklin.\" must display the following acknowledgement: 16c9fe00dcSJ.T. Conklin.\" This product includes software developed by Winning Strategies, Inc. 17c9fe00dcSJ.T. Conklin.\" 4. The name of the author may not be used to endorse or promote products 187d7ee86eSMike Pritchard.\" derived from this software without specific prior written permission 19c9fe00dcSJ.T. Conklin.\" 20c9fe00dcSJ.T. Conklin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21c9fe00dcSJ.T. Conklin.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22c9fe00dcSJ.T. Conklin.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23c9fe00dcSJ.T. Conklin.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24c9fe00dcSJ.T. Conklin.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25c9fe00dcSJ.T. Conklin.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26c9fe00dcSJ.T. Conklin.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27c9fe00dcSJ.T. Conklin.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28c9fe00dcSJ.T. Conklin.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29c9fe00dcSJ.T. Conklin.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30c9fe00dcSJ.T. Conklin.\" 312a456239SPeter Wemm.\" $FreeBSD$ 32c9fe00dcSJ.T. Conklin.\" 33*f07e4247SGarrett Wollman.Dd March 22, 2002 34c9fe00dcSJ.T. Conklin.Dt EXPR 1 35c9fe00dcSJ.T. Conklin.Os 36c9fe00dcSJ.T. Conklin.Sh NAME 37c9fe00dcSJ.T. Conklin.Nm expr 38c9fe00dcSJ.T. Conklin.Nd evaluate expression 39c9fe00dcSJ.T. Conklin.Sh SYNOPSIS 40be8b1497SRuslan Ermilov.Nm 41*f07e4247SGarrett Wollman.Op Fl \&- 42c9fe00dcSJ.T. Conklin.Ar expression 43c9fe00dcSJ.T. Conklin.Sh DESCRIPTION 44c9fe00dcSJ.T. ConklinThe 4515638d89SPhilippe Charnier.Nm 46c9fe00dcSJ.T. Conklinutility evaluates 47c9fe00dcSJ.T. Conklin.Ar expression 48c9fe00dcSJ.T. Conklinand writes the result on standard output. 49c9fe00dcSJ.T. Conklin.Pp 50*f07e4247SGarrett WollmanAll operators and operands must be passed as separate arguments. 51*f07e4247SGarrett WollmanSeveral of the operators have special meaning to command interpreters 52*f07e4247SGarrett Wollmanand must therefore be quoted appropriately. 53c9fe00dcSJ.T. Conklin.Pp 54*f07e4247SGarrett WollmanArithmetic operations are performed using signed integer math, 55*f07e4247SGarrett Wollmanin the largest integral type available in the C language. The 56*f07e4247SGarrett Wollman.Nm 57*f07e4247SGarrett Wollmanutility will detect arithmetic overflow and division by zero, and 58*f07e4247SGarrett Wollmanreturns with an exit status of 2 in those cases. If a numeric operand 59*f07e4247SGarrett Wollmanis specified which is so large as to overflow conversion to an integer, 60*f07e4247SGarrett Wollmanit is parsed as a string instead. All numeric operands are interpreted 61*f07e4247SGarrett Wollmanin base 10. 62*f07e4247SGarrett Wollman.Pp 63*f07e4247SGarrett WollmanOperators are listed below in order of increasing precedence; all 64*f07e4247SGarrett Wollmanare left-associative. 657d7ee86eSMike PritchardOperators with equal precedence are grouped within { } symbols. 66c9fe00dcSJ.T. Conklin.Bl -tag -width indent 67c9fe00dcSJ.T. Conklin.It Ar expr1 Li | Ar expr2 6815638d89SPhilippe CharnierReturn the evaluation of 69c9fe00dcSJ.T. Conklin.Ar expr1 70c9fe00dcSJ.T. Conklinif it is neither an empty string nor zero; 71c9fe00dcSJ.T. Conklinotherwise, returns the evaluation of 72c9fe00dcSJ.T. Conklin.Ar expr2 . 73c9fe00dcSJ.T. Conklin.It Ar expr1 Li & Ar expr2 7415638d89SPhilippe CharnierReturn the evaluation of 75c9fe00dcSJ.T. Conklin.Ar expr1 76c9fe00dcSJ.T. Conklinif neither expression evaluates to an empty string or zero; 77c9fe00dcSJ.T. Conklinotherwise, returns zero. 78c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 7915638d89SPhilippe CharnierReturn the results of integer comparison if both arguments are integers; 80d8c21a3eSJ.T. Conklinotherwise, returns the results of string comparison using the locale-specific 81d8c21a3eSJ.T. Conklincollation sequence. 82c9fe00dcSJ.T. ConklinThe result of each comparison is 1 if the specified relation is true, 83c9fe00dcSJ.T. Conklinor 0 if the relation is false. 84c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{+, -}" Ar expr2 8515638d89SPhilippe CharnierReturn the results of addition or subtraction of integer-valued arguments. 86c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{*, /, %}" Ar expr2 8715638d89SPhilippe CharnierReturn the results of multiplication, integer division, or remainder of integer-valued arguments. 88c9fe00dcSJ.T. Conklin.It Ar expr1 Li : Ar expr2 89c9fe00dcSJ.T. ConklinThe 9012e720d7SRuslan Ermilov.Dq \&: 91c9fe00dcSJ.T. Conklinoperator matches 92c9fe00dcSJ.T. Conklin.Ar expr1 93c9fe00dcSJ.T. Conklinagainst 94c9fe00dcSJ.T. Conklin.Ar expr2 , 95*f07e4247SGarrett Wollmanwhich must be a basic regular expression. 96*f07e4247SGarrett WollmanThe regular expression is anchored 972caac73eSJordan K. Hubbardto the beginning of the string with an implicit 98d8c21a3eSJ.T. Conklin.Dq ^ . 99c9fe00dcSJ.T. Conklin.Pp 100d8c21a3eSJ.T. ConklinIf the match succeeds and the pattern contains at least one regular 101d8c21a3eSJ.T. Conklinexpression subexpression 102c9fe00dcSJ.T. Conklin.Dq "\e(...\e)" , 103c9fe00dcSJ.T. Conklinthe string corresponding to 104c9fe00dcSJ.T. Conklin.Dq "\e1" 105c9fe00dcSJ.T. Conklinis returned; 106c9fe00dcSJ.T. Conklinotherwise the matching operator returns the number of characters matched. 107d8c21a3eSJ.T. ConklinIf the match fails and the pattern contains a regular expression subexpression 108d8c21a3eSJ.T. Conklinthe null string is returned; 109d8c21a3eSJ.T. Conklinotherwise 0. 110c9fe00dcSJ.T. Conklin.El 111c9fe00dcSJ.T. Conklin.Pp 112c9fe00dcSJ.T. ConklinParentheses are used for grouping in the usual manner. 113*f07e4247SGarrett Wollman.Pp 114*f07e4247SGarrett WollmanThis version of 115*f07e4247SGarrett Wollman.Nm 116*f07e4247SGarrett Wollmanadheres to the 117*f07e4247SGarrett Wollman.Tn POSIX 118*f07e4247SGarrett WollmanUtility Syntax Guidelines, which require that a leading argument beginning 119*f07e4247SGarrett Wollmanwith a minus sign be considered an option to the program. 120*f07e4247SGarrett WollmanThe standard 121*f07e4247SGarrett Wollman.Ql \&-- 122*f07e4247SGarrett Wollmansyntax may be used to prevent this interpretation. 123*f07e4247SGarrett WollmanHowever, many historic implementations of 124*f07e4247SGarrett Wollman.Nm , 125*f07e4247SGarrett Wollmanincluding the one in previous versions of 126*f07e4247SGarrett Wollman.Fx , 127*f07e4247SGarrett Wollmanwill not permit this syntax. 128*f07e4247SGarrett WollmanSee the examples below for portable ways to guarantee the correct 129*f07e4247SGarrett Wollmaninterpretation. 130*f07e4247SGarrett Wollman.Pp 131*f07e4247SGarrett WollmanThe 132*f07e4247SGarrett Wollman.Nm 133*f07e4247SGarrett Wollmanutility makes no lexical distinction between arguments which may be 134*f07e4247SGarrett Wollmanoperators and arguments which may be operands. 135*f07e4247SGarrett WollmanAn operand which is lexically identical to an operator will be considered a 136*f07e4247SGarrett Wollmansyntax error. 137*f07e4247SGarrett WollmanSee the examples below for a work-around. 138*f07e4247SGarrett Wollman.Pp 139*f07e4247SGarrett WollmanThe syntax of the 140*f07e4247SGarrett Wollman.Nm 141*f07e4247SGarrett Wollmancommand in general is historic and inconvenient. 142*f07e4247SGarrett WollmanNew applications are advised to use shell arithmetic rather than 143*f07e4247SGarrett Wollman.Nm . 144c9fe00dcSJ.T. Conklin.Sh EXAMPLES 145c9fe00dcSJ.T. Conklin.Bl -enum 146c9fe00dcSJ.T. Conklin.It 147*f07e4247SGarrett WollmanThe following example (in 148*f07e4247SGarrett Wollman.Xr sh 1 149*f07e4247SGarrett Wollmansyntax) adds one to the variable 150*f07e4247SGarrett Wollman.Va a . 151*f07e4247SGarrett Wollman.Dl a=$(expr $a + 1) 152*f07e4247SGarrett Wollman.Li 153*f07e4247SGarrett WollmanThis will fail if the value if 154*f07e4247SGarrett Wollman.Va a 155*f07e4247SGarrett Wollmanis a negative number. 156*f07e4247SGarrett WollmanTo protect negative values of 157*f07e4247SGarrett Wollman.Va a 158*f07e4247SGarrett Wollmanfrom being interpreted as options to the 159*f07e4247SGarrett Wollman.Nm 160*f07e4247SGarrett Wollmancommand, one might rearrange the expression: 161*f07e4247SGarrett Wollman.Dl a=$(expr 1 + $a) 162*f07e4247SGarrett Wollman.Li 163*f07e4247SGarrett WollmanMore generally, parenthesize possibly-negative values: 164*f07e4247SGarrett Wollman.Dl a=$(expr \e( $a \e) + 1) 165c9fe00dcSJ.T. Conklin.It 166*f07e4247SGarrett WollmanThe following example prints the filename portion of a pathname stored 167*f07e4247SGarrett Wollmanin variable 168*f07e4247SGarrett Wollman.Va a . 169*f07e4247SGarrett WollmanSince 170*f07e4247SGarrett Wollman.Va a 171*f07e4247SGarrett Wollmanmight represent the path 172*f07e4247SGarrett Wollman.Pa / , 173*f07e4247SGarrett Wollmanit is necessary to prevent it from being interpreted as the division operator. 174*f07e4247SGarrett WollmanThe 175*f07e4247SGarrett Wollman.Li // 176*f07e4247SGarrett Wollmancharacters resolve this ambiguity. 177*f07e4247SGarrett Wollman.Dl expr \*q//$a\*q \&: '.*/\e(.*\e)' 178c9fe00dcSJ.T. Conklin.It 179*f07e4247SGarrett WollmanThe following examples output the number of characters in variable 180*f07e4247SGarrett Wollman.Va a . 181*f07e4247SGarrett WollmanAgain, if 182*f07e4247SGarrett Wollman.Va a 183*f07e4247SGarrett Wollmanmight begin with a hyphen, it is necessary to prevent it from being 184*f07e4247SGarrett Wollmaninterpreted as an option to 185*f07e4247SGarrett Wollman.Nm . 186*f07e4247SGarrett WollmanIf the 187*f07e4247SGarrett Wollman.Nm 188*f07e4247SGarrett Wollmancommand conforms to 189*f07e4247SGarrett Wollman.St -p1003.1-2001 , 190*f07e4247SGarrett Wollmanthis is simple: 191*f07e4247SGarrett Wollman.Dl expr -- \*q$a\*q \&: \*q.*\*q 192*f07e4247SGarrett Wollman.Li 193*f07e4247SGarrett WollmanFor portability to older systems, however, a more complicated command 194*f07e4247SGarrett Wollmanis required: 195*f07e4247SGarrett Wollman.Dl expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1 196c9fe00dcSJ.T. Conklin.El 197c9fe00dcSJ.T. Conklin.Sh DIAGNOSTICS 198c9fe00dcSJ.T. ConklinThe 19915638d89SPhilippe Charnier.Nm 200c9fe00dcSJ.T. Conklinutility exits with one of the following values: 20115638d89SPhilippe Charnier.Bl -tag -width indent -compact 202c9fe00dcSJ.T. Conklin.It 0 203c9fe00dcSJ.T. Conklinthe expression is neither an empty string nor 0. 204c9fe00dcSJ.T. Conklin.It 1 205c9fe00dcSJ.T. Conklinthe expression is an empty string or 0. 206c9fe00dcSJ.T. Conklin.It 2 207c9fe00dcSJ.T. Conklinthe expression is invalid. 208c9fe00dcSJ.T. Conklin.El 209e5bf7bf4SJoseph Koshy.Sh SEE ALSO 210e5bf7bf4SJoseph Koshy.Xr sh 1 , 211e5bf7bf4SJoseph Koshy.Xr test 1 212c9fe00dcSJ.T. Conklin.Sh STANDARDS 213c9fe00dcSJ.T. ConklinThe 21415638d89SPhilippe Charnier.Nm 215d8c21a3eSJ.T. Conklinutility conforms to 216*f07e4247SGarrett Wollman.St -p1003.1-2001 . 217