1d8c21a3eSJ.T. Conklin.\" -*- nroff -*- 29ddb49cbSWarner Losh.\"- 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.\" 33f390d8d9SJilles Tjoelker.Dd September 9, 2010 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. 53*fa717604SStefan EßerAll integer operands are interpreted in base 10 and must consist of only 54*fa717604SStefan Eßeran optional leading minus sign followed by one or more digits (unless 55*fa717604SStefan Eßerless strict parsing has been enabled for backwards compatibilty with 56*fa717604SStefan Eßerprior versions of 57f07e4247SGarrett Wollman.Nm 58*fa717604SStefan Eßerin 59*fa717604SStefan Eßer.Fx ) . 60*fa717604SStefan Eßer.Pp 61*fa717604SStefan EßerArithmetic operations are performed using signed integer math with a 62*fa717604SStefan Eßerrange according to the C 63*fa717604SStefan Eßer.Vt intmax_t 64*fa717604SStefan Eßerdata type (the largest signed integral type available). 65*fa717604SStefan EßerAll conversions and operations are checked for overflow. 66*fa717604SStefan EßerOverflow results in program termination with an error message on stdout 67*fa717604SStefan Eßerand with an error status. 68*fa717604SStefan Eßer.Pp 69*fa717604SStefan EßerThe 701393277eSGarrett Wollman.Fl e 71*fa717604SStefan Eßeroption enables backwards compatible behaviour as detailed below. 72f07e4247SGarrett Wollman.Pp 73f07e4247SGarrett WollmanOperators are listed below in order of increasing precedence; all 74f07e4247SGarrett Wollmanare left-associative. 759f3cef91SRuslan ErmilovOperators with equal precedence are grouped within symbols 769f3cef91SRuslan Ermilov.Ql { 779f3cef91SRuslan Ermilovand 789f3cef91SRuslan Ermilov.Ql } . 79c9fe00dcSJ.T. Conklin.Bl -tag -width indent 80c9fe00dcSJ.T. Conklin.It Ar expr1 Li | Ar expr2 8115638d89SPhilippe CharnierReturn the evaluation of 82c9fe00dcSJ.T. Conklin.Ar expr1 83c9fe00dcSJ.T. Conklinif it is neither an empty string nor zero; 84c9fe00dcSJ.T. Conklinotherwise, returns the evaluation of 85*fa717604SStefan Eßer.Ar expr2 86*fa717604SStefan Eßerif it is not an empty string; 87*fa717604SStefan Eßerotherwise, returns zero. 88c9fe00dcSJ.T. Conklin.It Ar expr1 Li & Ar expr2 8915638d89SPhilippe CharnierReturn the evaluation of 90c9fe00dcSJ.T. Conklin.Ar expr1 91c9fe00dcSJ.T. Conklinif neither expression evaluates to an empty string or zero; 92c9fe00dcSJ.T. Conklinotherwise, returns zero. 93c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 9415638d89SPhilippe CharnierReturn the results of integer comparison if both arguments are integers; 95d8c21a3eSJ.T. Conklinotherwise, returns the results of string comparison using the locale-specific 96d8c21a3eSJ.T. Conklincollation sequence. 97c9fe00dcSJ.T. ConklinThe result of each comparison is 1 if the specified relation is true, 98c9fe00dcSJ.T. Conklinor 0 if the relation is false. 99c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{+, -}" Ar expr2 10015638d89SPhilippe CharnierReturn the results of addition or subtraction of integer-valued arguments. 101c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{*, /, %}" Ar expr2 10215638d89SPhilippe CharnierReturn the results of multiplication, integer division, or remainder of integer-valued arguments. 103c9fe00dcSJ.T. Conklin.It Ar expr1 Li : Ar expr2 104c9fe00dcSJ.T. ConklinThe 1059f3cef91SRuslan Ermilov.Dq Li \&: 106c9fe00dcSJ.T. Conklinoperator matches 107c9fe00dcSJ.T. Conklin.Ar expr1 108c9fe00dcSJ.T. Conklinagainst 109c9fe00dcSJ.T. Conklin.Ar expr2 , 110f07e4247SGarrett Wollmanwhich must be a basic regular expression. 111f07e4247SGarrett WollmanThe regular expression is anchored 1122caac73eSJordan K. Hubbardto the beginning of the string with an implicit 1139f3cef91SRuslan Ermilov.Dq Li ^ . 114c9fe00dcSJ.T. Conklin.Pp 115d8c21a3eSJ.T. ConklinIf the match succeeds and the pattern contains at least one regular 116d8c21a3eSJ.T. Conklinexpression subexpression 1179f3cef91SRuslan Ermilov.Dq Li "\e(...\e)" , 118c9fe00dcSJ.T. Conklinthe string corresponding to 1199f3cef91SRuslan Ermilov.Dq Li \e1 120c9fe00dcSJ.T. Conklinis returned; 121c9fe00dcSJ.T. Conklinotherwise the matching operator returns the number of characters matched. 122d8c21a3eSJ.T. ConklinIf the match fails and the pattern contains a regular expression subexpression 123d8c21a3eSJ.T. Conklinthe null string is returned; 124d8c21a3eSJ.T. Conklinotherwise 0. 125c9fe00dcSJ.T. Conklin.El 126c9fe00dcSJ.T. Conklin.Pp 127c9fe00dcSJ.T. ConklinParentheses are used for grouping in the usual manner. 128f07e4247SGarrett Wollman.Pp 129c9885518SGarrett WollmanThe 130c9885518SGarrett Wollman.Nm 131c9885518SGarrett Wollmanutility makes no lexical distinction between arguments which may be 132c9885518SGarrett Wollmanoperators and arguments which may be operands. 133c9885518SGarrett WollmanAn operand which is lexically identical to an operator will be considered a 134c9885518SGarrett Wollmansyntax error. 135c9885518SGarrett WollmanSee the examples below for a work-around. 136c9885518SGarrett Wollman.Pp 137c9885518SGarrett WollmanThe syntax of the 138c9885518SGarrett Wollman.Nm 139c9885518SGarrett Wollmancommand in general is historic and inconvenient. 140c9885518SGarrett WollmanNew applications are advised to use shell arithmetic rather than 141c9885518SGarrett Wollman.Nm . 142c9885518SGarrett Wollman.Ss Compatibility with previous implementations 143c9885518SGarrett WollmanUnless 14498d13c7eSRuslan Ermilov.Fx 14598d13c7eSRuslan Ermilov4.x 146c9885518SGarrett Wollmancompatibility is enabled, this version of 147f07e4247SGarrett Wollman.Nm 148f07e4247SGarrett Wollmanadheres to the 149e27480d6SRuslan Ermilov.Tn POSIX 150f07e4247SGarrett WollmanUtility Syntax Guidelines, which require that a leading argument beginning 151f07e4247SGarrett Wollmanwith a minus sign be considered an option to the program. 152f07e4247SGarrett WollmanThe standard 1539dfcbc3bSRuslan Ermilov.Fl Fl 154f07e4247SGarrett Wollmansyntax may be used to prevent this interpretation. 155f07e4247SGarrett WollmanHowever, many historic implementations of 156f07e4247SGarrett Wollman.Nm , 157f07e4247SGarrett Wollmanincluding the one in previous versions of 158f07e4247SGarrett Wollman.Fx , 159f07e4247SGarrett Wollmanwill not permit this syntax. 160f07e4247SGarrett WollmanSee the examples below for portable ways to guarantee the correct 161f07e4247SGarrett Wollmaninterpretation. 16294a48596SGarrett WollmanThe 163c9885518SGarrett Wollman.Xr check_utility_compat 3 164c9885518SGarrett Wollmanfunction (with a 165c9885518SGarrett Wollman.Fa utility 166c9885518SGarrett Wollmanargument of 167c9885518SGarrett Wollman.Dq Li expr ) 168*fa717604SStefan Eßeris used to determine whether backwards compatibility mode should be enabled. 169c9885518SGarrett WollmanThis feature is intended for use as a transition and debugging aid, when 17094a48596SGarrett Wollman.Nm 17194a48596SGarrett Wollmanis used in complex scripts which cannot easily be recast to avoid the 17294a48596SGarrett Wollmannon-portable usage. 173*fa717604SStefan EßerEnabling backwards compatibility mode also implicitly enables the 17494a48596SGarrett Wollman.Fl e 17594a48596SGarrett Wollmanoption, since this matches the historic behavior of 17694a48596SGarrett Wollman.Nm 17794a48596SGarrett Wollmanin 178*fa717604SStefan Eßer.Fx . This option makes number parsing less strict and permits leading 179*fa717604SStefan Eßerwhite space and an optional leading plus sign. In addition, empty operands 180*fa717604SStefan Eßerhave an implied value of zero in numeric context. 181c9885518SGarrett WollmanFor historical reasons, defining the environment variable 182c9885518SGarrett Wollman.Ev EXPR_COMPAT 183*fa717604SStefan Eßeralso enables backwards compatibility mode. 18496ab7da3SGarrett Wollman.Sh ENVIRONMENT 1859dfcbc3bSRuslan Ermilov.Bl -tag -width ".Ev EXPR_COMPAT" 18696ab7da3SGarrett Wollman.It Ev EXPR_COMPAT 187*fa717604SStefan EßerIf set, enables backwards compatibility mode. 18896ab7da3SGarrett Wollman.El 1896fca4c7cSRuslan Ermilov.Sh EXIT STATUS 1906fca4c7cSRuslan ErmilovThe 1916fca4c7cSRuslan Ermilov.Nm 1926fca4c7cSRuslan Ermilovutility exits with one of the following values: 1936fca4c7cSRuslan Ermilov.Bl -tag -width indent -compact 1946fca4c7cSRuslan Ermilov.It 0 1956fca4c7cSRuslan Ermilovthe expression is neither an empty string nor 0. 1966fca4c7cSRuslan Ermilov.It 1 1976fca4c7cSRuslan Ermilovthe expression is an empty string or 0. 1986fca4c7cSRuslan Ermilov.It 2 1996fca4c7cSRuslan Ermilovthe expression is invalid. 2006fca4c7cSRuslan Ermilov.El 201c9fe00dcSJ.T. Conklin.Sh EXAMPLES 202f477880aSGarrett Wollman.Bl -bullet 203c9fe00dcSJ.T. Conklin.It 204f07e4247SGarrett WollmanThe following example (in 205f07e4247SGarrett Wollman.Xr sh 1 206f07e4247SGarrett Wollmansyntax) adds one to the variable 207f477880aSGarrett Wollman.Va a : 2089dfcbc3bSRuslan Ermilov.Dl "a=$(expr $a + 1)" 209f477880aSGarrett Wollman.It 210dc2f94a3SGarrett WollmanThis will fail if the value of 211f07e4247SGarrett Wollman.Va a 212f07e4247SGarrett Wollmanis a negative number. 213f07e4247SGarrett WollmanTo protect negative values of 214f07e4247SGarrett Wollman.Va a 215f07e4247SGarrett Wollmanfrom being interpreted as options to the 216f07e4247SGarrett Wollman.Nm 217f07e4247SGarrett Wollmancommand, one might rearrange the expression: 2189dfcbc3bSRuslan Ermilov.Dl "a=$(expr 1 + $a)" 219f477880aSGarrett Wollman.It 220f07e4247SGarrett WollmanMore generally, parenthesize possibly-negative values: 2219dfcbc3bSRuslan Ermilov.Dl "a=$(expr \e( $a \e) + 1)" 222c9fe00dcSJ.T. Conklin.It 223f390d8d9SJilles TjoelkerWith shell arithmetic, no escaping is required: 224f390d8d9SJilles Tjoelker.Dl "a=$((a + 1))" 225f390d8d9SJilles Tjoelker.It 2261393277eSGarrett WollmanThis example prints the filename portion of a pathname stored 227f07e4247SGarrett Wollmanin variable 228f07e4247SGarrett Wollman.Va a . 229f07e4247SGarrett WollmanSince 230f07e4247SGarrett Wollman.Va a 231f07e4247SGarrett Wollmanmight represent the path 232f07e4247SGarrett Wollman.Pa / , 233f07e4247SGarrett Wollmanit is necessary to prevent it from being interpreted as the division operator. 234f07e4247SGarrett WollmanThe 235f07e4247SGarrett Wollman.Li // 236f07e4247SGarrett Wollmancharacters resolve this ambiguity. 2379dfcbc3bSRuslan Ermilov.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'" 238f390d8d9SJilles Tjoelker.It 239f390d8d9SJilles TjoelkerWith modern 240f390d8d9SJilles Tjoelker.Xr sh 1 241f390d8d9SJilles Tjoelkersyntax, 242f390d8d9SJilles Tjoelker.Dl "\*q${a##*/}\*q" 243f390d8d9SJilles Tjoelkerexpands to the same value. 244f477880aSGarrett Wollman.El 245f477880aSGarrett Wollman.Pp 246f07e4247SGarrett WollmanThe following examples output the number of characters in variable 247f07e4247SGarrett Wollman.Va a . 248f07e4247SGarrett WollmanAgain, if 249f07e4247SGarrett Wollman.Va a 250f07e4247SGarrett Wollmanmight begin with a hyphen, it is necessary to prevent it from being 251f07e4247SGarrett Wollmaninterpreted as an option to 252f390d8d9SJilles Tjoelker.Nm , 253f390d8d9SJilles Tjoelkerand 254f390d8d9SJilles Tjoelker.Va a 255f390d8d9SJilles Tjoelkermight be interpreted as an operator. 256f477880aSGarrett Wollman.Bl -bullet 257f477880aSGarrett Wollman.It 258f390d8d9SJilles TjoelkerTo deal with all of this, a complicated command 259f07e4247SGarrett Wollmanis required: 2609dfcbc3bSRuslan Ermilov.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1" 261f390d8d9SJilles Tjoelker.It 262f390d8d9SJilles TjoelkerWith modern 263f390d8d9SJilles Tjoelker.Xr sh 1 264f390d8d9SJilles Tjoelkersyntax, this can be done much more easily: 265f390d8d9SJilles Tjoelker.Dl "${#a}" 266f390d8d9SJilles Tjoelkerexpands to the required number. 267c9fe00dcSJ.T. Conklin.El 268e5bf7bf4SJoseph Koshy.Sh SEE ALSO 269e5bf7bf4SJoseph Koshy.Xr sh 1 , 270c9885518SGarrett Wollman.Xr test 1 , 271c9885518SGarrett Wollman.Xr check_utility_compat 3 272c9fe00dcSJ.T. Conklin.Sh STANDARDS 273c9fe00dcSJ.T. ConklinThe 27415638d89SPhilippe Charnier.Nm 275d8c21a3eSJ.T. Conklinutility conforms to 276*fa717604SStefan Eßer.St -p1003.1-2008 , 277*fa717604SStefan Eßerprovided that backwards compatibility mode is not enabled. 278*fa717604SStefan Eßer.Pp 279*fa717604SStefan EßerBackwards compatibility mode performs less strict checks of numeric arguments: 280*fa717604SStefan Eßer.Bl -bullet 281*fa717604SStefan Eßer.It 282*fa717604SStefan EßerAn empty operand string is interpreted as 0. 283*fa717604SStefan Eßer.El 284*fa717604SStefan Eßer.Bl -bullet 285*fa717604SStefan Eßer.It 286*fa717604SStefan EßerLeading white space and/or a plus sign before an otherwise valid positive 287*fa717604SStefan Eßernumberic operand are allowed and will be ignored. 288*fa717604SStefan Eßer.El 289*fa717604SStefan Eßer.Pp 290*fa717604SStefan EßerThe extended arithmetic range and overflow checks do not conflict with 291*fa717604SStefan EßerPOSIX's requirement that arithmetic be done using signed longs, since 292*fa717604SStefan Eßerthey only make a difference to the result in cases where using signed 293*fa717604SStefan Eßerlongs would give undefined behavior. 294*fa717604SStefan Eßer.Pp 295*fa717604SStefan EßerAccording to the 296*fa717604SStefan Eßer.Tn POSIX 297*fa717604SStefan Eßerstandard, the use of string arguments 298*fa717604SStefan Eßer.Va length , 299*fa717604SStefan Eßer.Va substr , 300*fa717604SStefan Eßer.Va index , 301*fa717604SStefan Eßeror 302*fa717604SStefan Eßer.Va match 303*fa717604SStefan Eßerproduces undefined results. In this version of 304*fa717604SStefan Eßer.Nm , 305*fa717604SStefan Eßerthese arguments are treated just as their respective string values. 306*fa717604SStefan Eßer.Pp 3071393277eSGarrett WollmanThe 3081393277eSGarrett Wollman.Fl e 3091393277eSGarrett Wollmanflag is an extension. 310