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.\" 331393277eSGarrett Wollman.Dd May 10, 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 411393277eSGarrett Wollman.Op Fl e 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 50f07e4247SGarrett WollmanAll operators and operands must be passed as separate arguments. 51f07e4247SGarrett WollmanSeveral of the operators have special meaning to command interpreters 52f07e4247SGarrett Wollmanand must therefore be quoted appropriately. 531393277eSGarrett WollmanAll integer operands are interpreted in base 10. 54c9fe00dcSJ.T. Conklin.Pp 551393277eSGarrett WollmanArithmetic operations are performed using signed integer math. 561393277eSGarrett WollmanIf the 571393277eSGarrett Wollman.Fl e 581393277eSGarrett Wollmanflag is specified, arithmetic uses the C 591393277eSGarrett Wollman.Ql intmax_t 601393277eSGarrett Wollmandata type (the largest integral type available), and 61f07e4247SGarrett Wollman.Nm 621393277eSGarrett Wollmanwill detect arithmetic overflow and return an error indication. 631393277eSGarrett WollmanIf a numeric operand is specified which is so large as to overflow 641393277eSGarrett Wollmanconversion to an integer, it is parsed as a string instead. 651393277eSGarrett WollmanIf 661393277eSGarrett Wollman.Fl e 671393277eSGarrett Wollmanis not specified, arithmetic operations and parsing of integer 681393277eSGarrett Wollmanarguments will overflow silently according to the rules of the C 691393277eSGarrett Wollmanstandard, and integer computations will be performed using the 701393277eSGarrett Wollman.Ql long 711393277eSGarrett Wollmandata type. 72f07e4247SGarrett Wollman.Pp 73f07e4247SGarrett WollmanOperators are listed below in order of increasing precedence; all 74f07e4247SGarrett Wollmanare left-associative. 757d7ee86eSMike PritchardOperators with equal precedence are grouped within { } symbols. 76c9fe00dcSJ.T. Conklin.Bl -tag -width indent 77c9fe00dcSJ.T. Conklin.It Ar expr1 Li | Ar expr2 7815638d89SPhilippe CharnierReturn the evaluation of 79c9fe00dcSJ.T. Conklin.Ar expr1 80c9fe00dcSJ.T. Conklinif it is neither an empty string nor zero; 81c9fe00dcSJ.T. Conklinotherwise, returns the evaluation of 82c9fe00dcSJ.T. Conklin.Ar expr2 . 83c9fe00dcSJ.T. Conklin.It Ar expr1 Li & Ar expr2 8415638d89SPhilippe CharnierReturn the evaluation of 85c9fe00dcSJ.T. Conklin.Ar expr1 86c9fe00dcSJ.T. Conklinif neither expression evaluates to an empty string or zero; 87c9fe00dcSJ.T. Conklinotherwise, returns zero. 88c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 8915638d89SPhilippe CharnierReturn the results of integer comparison if both arguments are integers; 90d8c21a3eSJ.T. Conklinotherwise, returns the results of string comparison using the locale-specific 91d8c21a3eSJ.T. Conklincollation sequence. 92c9fe00dcSJ.T. ConklinThe result of each comparison is 1 if the specified relation is true, 93c9fe00dcSJ.T. Conklinor 0 if the relation is false. 94c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{+, -}" Ar expr2 9515638d89SPhilippe CharnierReturn the results of addition or subtraction of integer-valued arguments. 96c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{*, /, %}" Ar expr2 9715638d89SPhilippe CharnierReturn the results of multiplication, integer division, or remainder of integer-valued arguments. 98c9fe00dcSJ.T. Conklin.It Ar expr1 Li : Ar expr2 99c9fe00dcSJ.T. ConklinThe 10012e720d7SRuslan Ermilov.Dq \&: 101c9fe00dcSJ.T. Conklinoperator matches 102c9fe00dcSJ.T. Conklin.Ar expr1 103c9fe00dcSJ.T. Conklinagainst 104c9fe00dcSJ.T. Conklin.Ar expr2 , 105f07e4247SGarrett Wollmanwhich must be a basic regular expression. 106f07e4247SGarrett WollmanThe regular expression is anchored 1072caac73eSJordan K. Hubbardto the beginning of the string with an implicit 108d8c21a3eSJ.T. Conklin.Dq ^ . 109c9fe00dcSJ.T. Conklin.Pp 110d8c21a3eSJ.T. ConklinIf the match succeeds and the pattern contains at least one regular 111d8c21a3eSJ.T. Conklinexpression subexpression 112c9fe00dcSJ.T. Conklin.Dq "\e(...\e)" , 113c9fe00dcSJ.T. Conklinthe string corresponding to 114c9fe00dcSJ.T. Conklin.Dq "\e1" 115c9fe00dcSJ.T. Conklinis returned; 116c9fe00dcSJ.T. Conklinotherwise the matching operator returns the number of characters matched. 117d8c21a3eSJ.T. ConklinIf the match fails and the pattern contains a regular expression subexpression 118d8c21a3eSJ.T. Conklinthe null string is returned; 119d8c21a3eSJ.T. Conklinotherwise 0. 120c9fe00dcSJ.T. Conklin.El 121c9fe00dcSJ.T. Conklin.Pp 122c9fe00dcSJ.T. ConklinParentheses are used for grouping in the usual manner. 123f07e4247SGarrett Wollman.Pp 12496ab7da3SGarrett WollmanUnless the 12596ab7da3SGarrett Wollman.Ev EXPR_COMPAT 12696ab7da3SGarrett Wollmanvariable is defined in the process environment, this version of 127f07e4247SGarrett Wollman.Nm 128f07e4247SGarrett Wollmanadheres to the 129f07e4247SGarrett Wollman.Tn POSIX 130f07e4247SGarrett WollmanUtility Syntax Guidelines, which require that a leading argument beginning 131f07e4247SGarrett Wollmanwith a minus sign be considered an option to the program. 132f07e4247SGarrett WollmanThe standard 133f07e4247SGarrett Wollman.Ql \&-- 134f07e4247SGarrett Wollmansyntax may be used to prevent this interpretation. 135f07e4247SGarrett WollmanHowever, many historic implementations of 136f07e4247SGarrett Wollman.Nm , 137f07e4247SGarrett Wollmanincluding the one in previous versions of 138f07e4247SGarrett Wollman.Fx , 139f07e4247SGarrett Wollmanwill not permit this syntax. 140f07e4247SGarrett WollmanSee the examples below for portable ways to guarantee the correct 141f07e4247SGarrett Wollmaninterpretation. 142f07e4247SGarrett Wollman.Pp 143f07e4247SGarrett WollmanThe 144f07e4247SGarrett Wollman.Nm 145f07e4247SGarrett Wollmanutility makes no lexical distinction between arguments which may be 146f07e4247SGarrett Wollmanoperators and arguments which may be operands. 147f07e4247SGarrett WollmanAn operand which is lexically identical to an operator will be considered a 148f07e4247SGarrett Wollmansyntax error. 149f07e4247SGarrett WollmanSee the examples below for a work-around. 150f07e4247SGarrett Wollman.Pp 151f07e4247SGarrett WollmanThe syntax of the 152f07e4247SGarrett Wollman.Nm 153f07e4247SGarrett Wollmancommand in general is historic and inconvenient. 154f07e4247SGarrett WollmanNew applications are advised to use shell arithmetic rather than 155f07e4247SGarrett Wollman.Nm . 15696ab7da3SGarrett Wollman.Sh ENVIRONMENT 15796ab7da3SGarrett Wollman.Bl -tag -compact -width EXPR_COMPAT 15896ab7da3SGarrett Wollman.It Ev EXPR_COMPAT 15996ab7da3SGarrett WollmanIf set, 16096ab7da3SGarrett Wollman.Nm 16196ab7da3SGarrett Wollmanwill emulate historic 16296ab7da3SGarrett Wollman.Nm 16396ab7da3SGarrett Wollmanimplementations which did not obey the Utility Syntax Guidelines. 16496ab7da3SGarrett Wollman.El 165c9fe00dcSJ.T. Conklin.Sh EXAMPLES 166f477880aSGarrett Wollman.Bl -bullet 167c9fe00dcSJ.T. Conklin.It 168f07e4247SGarrett WollmanThe following example (in 169f07e4247SGarrett Wollman.Xr sh 1 170f07e4247SGarrett Wollmansyntax) adds one to the variable 171f477880aSGarrett Wollman.Va a : 172f07e4247SGarrett Wollman.Dl a=$(expr $a + 1) 173f477880aSGarrett Wollman.It 174dc2f94a3SGarrett WollmanThis will fail if the value of 175f07e4247SGarrett Wollman.Va a 176f07e4247SGarrett Wollmanis a negative number. 177f07e4247SGarrett WollmanTo protect negative values of 178f07e4247SGarrett Wollman.Va a 179f07e4247SGarrett Wollmanfrom being interpreted as options to the 180f07e4247SGarrett Wollman.Nm 181f07e4247SGarrett Wollmancommand, one might rearrange the expression: 182f07e4247SGarrett Wollman.Dl a=$(expr 1 + $a) 183f477880aSGarrett Wollman.It 184f07e4247SGarrett WollmanMore generally, parenthesize possibly-negative values: 185f07e4247SGarrett Wollman.Dl a=$(expr \e( $a \e) + 1) 186c9fe00dcSJ.T. Conklin.It 1871393277eSGarrett WollmanThis example prints the filename portion of a pathname stored 188f07e4247SGarrett Wollmanin variable 189f07e4247SGarrett Wollman.Va a . 190f07e4247SGarrett WollmanSince 191f07e4247SGarrett Wollman.Va a 192f07e4247SGarrett Wollmanmight represent the path 193f07e4247SGarrett Wollman.Pa / , 194f07e4247SGarrett Wollmanit is necessary to prevent it from being interpreted as the division operator. 195f07e4247SGarrett WollmanThe 196f07e4247SGarrett Wollman.Li // 197f07e4247SGarrett Wollmancharacters resolve this ambiguity. 198f07e4247SGarrett Wollman.Dl expr \*q//$a\*q \&: '.*/\e(.*\e)' 199f477880aSGarrett Wollman.El 200f477880aSGarrett Wollman.Pp 201f07e4247SGarrett WollmanThe following examples output the number of characters in variable 202f07e4247SGarrett Wollman.Va a . 203f07e4247SGarrett WollmanAgain, if 204f07e4247SGarrett Wollman.Va a 205f07e4247SGarrett Wollmanmight begin with a hyphen, it is necessary to prevent it from being 206f07e4247SGarrett Wollmaninterpreted as an option to 207f07e4247SGarrett Wollman.Nm . 208f477880aSGarrett Wollman.Bl -bullet 209f477880aSGarrett Wollman.It 210f07e4247SGarrett WollmanIf the 211f07e4247SGarrett Wollman.Nm 212f07e4247SGarrett Wollmancommand conforms to 213f07e4247SGarrett Wollman.St -p1003.1-2001 , 214f07e4247SGarrett Wollmanthis is simple: 215f07e4247SGarrett Wollman.Dl expr -- \*q$a\*q \&: \*q.*\*q 216f477880aSGarrett Wollman.It 217f07e4247SGarrett WollmanFor portability to older systems, however, a more complicated command 218f07e4247SGarrett Wollmanis required: 219f07e4247SGarrett Wollman.Dl expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1 220c9fe00dcSJ.T. Conklin.El 221c9fe00dcSJ.T. Conklin.Sh DIAGNOSTICS 222c9fe00dcSJ.T. ConklinThe 22315638d89SPhilippe Charnier.Nm 224c9fe00dcSJ.T. Conklinutility exits with one of the following values: 22515638d89SPhilippe Charnier.Bl -tag -width indent -compact 226c9fe00dcSJ.T. Conklin.It 0 227c9fe00dcSJ.T. Conklinthe expression is neither an empty string nor 0. 228c9fe00dcSJ.T. Conklin.It 1 229c9fe00dcSJ.T. Conklinthe expression is an empty string or 0. 230c9fe00dcSJ.T. Conklin.It 2 231c9fe00dcSJ.T. Conklinthe expression is invalid. 232c9fe00dcSJ.T. Conklin.El 233e5bf7bf4SJoseph Koshy.Sh SEE ALSO 234e5bf7bf4SJoseph Koshy.Xr sh 1 , 235e5bf7bf4SJoseph Koshy.Xr test 1 236c9fe00dcSJ.T. Conklin.Sh STANDARDS 237c9fe00dcSJ.T. ConklinThe 23815638d89SPhilippe Charnier.Nm 239d8c21a3eSJ.T. Conklinutility conforms to 24096ab7da3SGarrett Wollman.St -p1003.1-2001 , 24196ab7da3SGarrett Wollmanprovided that the 24296ab7da3SGarrett Wollman.Ev EXPR_COMPAT 24396ab7da3SGarrett Wollmanenvironment variable is not defined. 2441393277eSGarrett WollmanThe 2451393277eSGarrett Wollman.Fl e 2461393277eSGarrett Wollmanflag is an extension. 247