xref: /netbsd-src/bin/expr/expr.1 (revision ec184f3bfb1748cb9888890166d23cf91dc27cf8)
1*ec184f3bShgutch.\"	$NetBSD: expr.1,v 1.38 2022/08/28 10:48:16 hgutch Exp $
249f0ad86Scgd.\"
383f8b02dSjdolecek.\" Copyright (c) 2000,2003 The NetBSD Foundation, Inc.
483f8b02dSjdolecek.\" All rights reserved.
578bf9d40Sjtc.\"
683f8b02dSjdolecek.\" This code is derived from software contributed to The NetBSD Foundation
783f8b02dSjdolecek.\" by J.T. Conklin <jtc@NetBSD.org> and Jaromir Dolecek <jdolecek@NetBSD.org>.
883f8b02dSjdolecek.\"
983f8b02dSjdolecek.\" Redistribution and use in source and binary forms, with or without
1083f8b02dSjdolecek.\" modification, are permitted provided that the following conditions
1183f8b02dSjdolecek.\" are met:
1283f8b02dSjdolecek.\" 1. Redistributions of source code must retain the above copyright
1383f8b02dSjdolecek.\"    notice, this list of conditions and the following disclaimer.
1483f8b02dSjdolecek.\" 2. Redistributions in binary form must reproduce the above copyright
1583f8b02dSjdolecek.\"    notice, this list of conditions and the following disclaimer in the
1683f8b02dSjdolecek.\"    documentation and/or other materials provided with the distribution.
1783f8b02dSjdolecek.\"
1883f8b02dSjdolecek.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1983f8b02dSjdolecek.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2083f8b02dSjdolecek.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2183f8b02dSjdolecek.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2283f8b02dSjdolecek.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2383f8b02dSjdolecek.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2483f8b02dSjdolecek.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2583f8b02dSjdolecek.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2683f8b02dSjdolecek.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2783f8b02dSjdolecek.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2883f8b02dSjdolecek.\" POSSIBILITY OF SUCH DAMAGE.
2983f8b02dSjdolecek.\"
300f2a0901Ssevan.Dd August 23, 2016
3178bf9d40Sjtc.Dt EXPR 1
3278bf9d40Sjtc.Os
3378bf9d40Sjtc.Sh NAME
3478bf9d40Sjtc.Nm expr
3578bf9d40Sjtc.Nd evaluate expression
3678bf9d40Sjtc.Sh SYNOPSIS
37c3872193Senami.Nm
3878bf9d40Sjtc.Ar expression
3978bf9d40Sjtc.Sh DESCRIPTION
4078bf9d40SjtcThe
41c3872193Senami.Nm
4278bf9d40Sjtcutility evaluates
4378bf9d40Sjtc.Ar expression
4478bf9d40Sjtcand writes the result on standard output.
4578bf9d40Sjtc.Pp
4678bf9d40SjtcAll operators are separate arguments to the
47c3872193Senami.Nm
4878bf9d40Sjtcutility.
4978bf9d40SjtcCharacters special to the command interpreter must be escaped.
5078bf9d40Sjtc.Pp
51e04c8e07SjtcOperators are listed below in order of increasing precedence.
52e04c8e07SjtcOperators with equal precedence are grouped within { } symbols.
5378bf9d40Sjtc.Bl -tag -width indent
5452bf796fSjoerg.It Ar expr1 Li \&| Ar expr2
5578bf9d40SjtcReturns the evaluation of
5678bf9d40Sjtc.Ar expr1
5778bf9d40Sjtcif it is neither an empty string nor zero;
5878bf9d40Sjtcotherwise, returns the evaluation of
5978bf9d40Sjtc.Ar expr2 .
6001869ca4Swiz.It Ar expr1 Li & Ar expr2
6178bf9d40SjtcReturns the evaluation of
6278bf9d40Sjtc.Ar expr1
6378bf9d40Sjtcif neither expression evaluates to an empty string or zero;
6478bf9d40Sjtcotherwise, returns zero.
65*ec184f3bShgutch.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
66e04c8e07SjtcReturns the results of integer comparison if both arguments are integers;
6704b0d37bSjtcotherwise, returns the results of string comparison using the locale-specific
6804b0d37bSjtccollation sequence.
6978bf9d40SjtcThe result of each comparison is 1 if the specified relation is true,
7078bf9d40Sjtcor 0 if the relation is false.
7178bf9d40Sjtc.It Ar expr1 Li "{+, -}" Ar expr2
7278bf9d40SjtcReturns the results of addition or subtraction of integer-valued arguments.
7378bf9d40Sjtc.It Ar expr1 Li "{*, /, %}" Ar expr2
7478bf9d40SjtcReturns the results of multiplication, integer division, or remainder of integer-valued arguments.
7552bf796fSjoerg.It Ar expr1 Li \&: Ar expr2
7678bf9d40SjtcThe
77880b4d21Swiz.Dq \&:
7878bf9d40Sjtcoperator matches
7978bf9d40Sjtc.Ar expr1
8078bf9d40Sjtcagainst
8178bf9d40Sjtc.Ar expr2 ,
8214dfaa4bSwizwhich must be a regular expression.
8314dfaa4bSwizThe regular expression is anchored
84e04c8e07Sjtcto the beginning of the string with an implicit
85441c51a9Sjtc.Dq ^ .
8678bf9d40Sjtc.Pp
87441c51a9SjtcIf the match succeeds and the pattern contains at least one regular
88441c51a9Sjtcexpression subexpression
8978bf9d40Sjtc.Dq "\e(...\e)" ,
9078bf9d40Sjtcthe string corresponding to
9178bf9d40Sjtc.Dq "\e1"
9278bf9d40Sjtcis returned;
9378bf9d40Sjtcotherwise the matching operator returns the number of characters matched.
94441c51a9SjtcIf the match fails and the pattern contains a regular expression subexpression
95441c51a9Sjtcthe null string is returned;
96441c51a9Sjtcotherwise 0.
97a423ef4aSwiz.It "( " Ar expr No " )"
989ad5bf10SjdolecekParentheses are used for grouping in the usual manner.
9978bf9d40Sjtc.El
10078bf9d40Sjtc.Pp
101a423ef4aSwizAdditionally, the following keywords are recognized:
102c92704dcSjdolecek.Bl -tag -width indent
103c92704dcSjdolecek.It length Ar expr
104a423ef4aSwizReturns the length of the specified string in bytes.
105c92704dcSjdolecek.El
106c92704dcSjdolecek.Pp
1079ad5bf10SjdolecekOperator precedence (from highest to lowest):
1089ad5bf10Sjdolecek.Bl -enum -compact -offset indent
1099ad5bf10Sjdolecek.It
1109ad5bf10Sjdolecekparentheses
1119ad5bf10Sjdolecek.It
112c92704dcSjdoleceklength
113c92704dcSjdolecek.It
114880b4d21Swiz.Dq \&:
1153b932d2fSjdolecek.It
1163b932d2fSjdolecek.Dq "*" ,
1173b932d2fSjdolecek.Dq "/" ,
1183b932d2fSjdolecekand
1193b932d2fSjdolecek.Dq "%"
1203b932d2fSjdolecek.It
1213b932d2fSjdolecek.Dq "+"
1223b932d2fSjdolecekand
1233b932d2fSjdolecek.Dq "-"
1243b932d2fSjdolecek.It
1253b932d2fSjdolecekcompare operators
1269ad5bf10Sjdolecek.It
12701869ca4Swiz.Dq &
1289ad5bf10Sjdolecek.It
12952bf796fSjoerg.Dq \&|
1309ad5bf10Sjdolecek.El
131e0ab876eSwiz.Sh EXIT STATUS
132e0ab876eSwizThe
133e0ab876eSwiz.Nm
134e0ab876eSwizutility exits with one of the following values:
135e0ab876eSwiz.Bl -tag -width Ds -compact
136e0ab876eSwiz.It 0
137e0ab876eSwizthe expression is neither an empty string nor 0.
138e0ab876eSwiz.It 1
139e0ab876eSwizthe expression is an empty string or 0.
140e0ab876eSwiz.It 2
141e0ab876eSwizthe expression is invalid.
14201869ca4Swiz.It >2
143e0ab876eSwizan error occurred (such as memory allocation failure).
144e0ab876eSwiz.El
14578bf9d40Sjtc.Sh EXAMPLES
14678bf9d40Sjtc.Bl -enum
14778bf9d40Sjtc.It
1488630b900SwizThe following example adds one to variable
1498630b900Swiz.Dq a :
15078bf9d40Sjtc.Dl a=`expr $a + 1`
15178bf9d40Sjtc.It
1529653d2d6SdhollandThe following example returns zero, due to subtraction having higher precedence
1538630b900Swizthan the
15401869ca4Swiz.Dq &
1558630b900Swizoperator:
15601869ca4Swiz.Dl expr 1 '&' 1 - 1
1579ad5bf10Sjdolecek.It
15878bf9d40SjtcThe following example returns the filename portion of a pathname stored
1598630b900Swizin variable
1608630b900Swiz.Dq a :
1613a4441e3Sjdolecek.Dl expr "/$a" Li : '.*/\e(.*\e)'
16278bf9d40Sjtc.It
1638630b900SwizThe following example returns the number of characters in variable
1648630b900Swiz.Dq a :
16578bf9d40Sjtc.Dl expr $a Li : '.*'
16678bf9d40Sjtc.El
1679ad5bf10Sjdolecek.Sh COMPATIBILITY
1683a4441e3SjdolecekThis implementation of
1693a4441e3Sjdolecek.Nm
17083237da0Sperryinternally uses 64 bit representation of integers and checks for
17114dfaa4bSwizover- and underflows.
1728630b900SwizIt also treats
1738630b900Swiz.Dq /
1748630b900Swiz(the division mark) and option
1758630b900Swiz.Dq --
1768630b900Swizcorrectly depending upon context.
1773a4441e3Sjdolecek.Pp
1783a4441e3Sjdolecek.Nm
1793a4441e3Sjdolecekon other systems (including
1803a4441e3Sjdolecek.Nx
1813a4441e3Sjdolecekup to and including
1823a4441e3Sjdolecek.Nx 1.5 )
1836af689d3Swizmight not be so graceful.
18414dfaa4bSwizArithmetic results might be arbitrarily
1853a4441e3Sjdoleceklimited on such systems, most commonly to 32 bit quantities.
1863a4441e3SjdolecekThis means such
1873a4441e3Sjdolecek.Nm
1883a4441e3Sjdolecekcan only process values between -2147483648 and +2147483647.
1893a4441e3Sjdolecek.Pp
1903a4441e3SjdolecekOn other systems,
1913a4441e3Sjdolecek.Nm
1923a4441e3Sjdolecekmight also not work correctly for regular expressions where
1938630b900Swizeither side contains
1948630b900Swiz.Dq /
1958630b900Swiz(a single forward slash), like this:
1963a4441e3Sjdolecek.Bd -literal -offset indent
1973a4441e3Sjdolecekexpr / : '.*/\e(.*\e)'
1983a4441e3Sjdolecek.Ed
1993a4441e3Sjdolecek.Pp
2008630b900SwizIf this is the case, you might use
2018630b900Swiz.Dq //
2028630b900Swiz(a double forward slash)
20383237da0Sperryto avoid confusion with the division operator:
2043a4441e3Sjdolecek.Bd -literal -offset indent
2053a4441e3Sjdolecekexpr "//$a" : '.*/\e(.*\e)'
2063a4441e3Sjdolecek.Ed
2079ad5bf10Sjdolecek.Pp
2089ad5bf10SjdolecekAccording to
2099ad5bf10Sjdolecek.St -p1003.2 ,
2109ad5bf10Sjdolecek.Nm
2118630b900Swizhas to recognize special option
2128630b900Swiz.Dq -- ,
2138630b900Swiztreat it as a delimiter to mark the end of command
2148630b900Swizline options, and ignore it.
2159ad5bf10SjdolecekSome
2169ad5bf10Sjdolecek.Nm
2170f2a0901Ssevanimplementations do not recognize it at all; others
2189ad5bf10Sjdolecekmight ignore it even in cases where doing so results in syntax
21914dfaa4bSwizerror.
22014dfaa4bSwizThere should be same result for both following examples,
2219ad5bf10Sjdolecekbut it might not always be:
2229ad5bf10Sjdolecek.Bl -enum -compact -offset indent
2239ad5bf10Sjdolecek.It
2249ad5bf10Sjdolecekexpr -- : .
2259ad5bf10Sjdolecek.It
2269ad5bf10Sjdolecekexpr -- -- : .
2279ad5bf10Sjdolecek.El
22883237da0SperryAlthough
2299ad5bf10Sjdolecek.Nx
2309ad5bf10Sjdolecek.Nm
23183237da0Sperryhandles both cases correctly, you should not depend on this behavior
2328630b900Swizfor portability reasons and avoid passing a bare
2338630b900Swiz.Dq --
2348630b900Swizas the first
2359ad5bf10Sjdolecekargument.
23652bf796fSjoerg.Sh STANDARDS
23752bf796fSjoergThe
23852bf796fSjoerg.Nm
23952bf796fSjoergutility conforms to
24052bf796fSjoerg.St -p1003.2 .
24152bf796fSjoergThe
24252bf796fSjoerg.Ar length
24352bf796fSjoergkeyword is an extension for compatibility with GNU
24452bf796fSjoerg.Nm .
2450f2a0901Ssevan.Sh HISTORY
2460f2a0901SsevanAn
2470f2a0901Ssevan.Nm
2480f2a0901Ssevanutility first appeared in the Programmer's Workbench (PWB/UNIX).
24903fb25d4SsevanA public domain version of
2500f2a0901Ssevan.Nm
2510f2a0901Ssevanwritten by
2520f2a0901Ssevan.An Pace Willisson
2530f2a0901Ssevan.Aq pace@blitz.com
2540f2a0901Ssevanappeared in
2550f2a0901Ssevan.Bx 386 0.1 .
25652bf796fSjoerg.Sh AUTHORS
2570f2a0901SsevanInitial implementation by
25803fb25d4Ssevan.An Pace Willisson Aq Mt pace@blitz.com
2590f2a0901Ssevanwas largely rewritten by
2609e840150Ssevan.An -nosplit
26103fb25d4Ssevan.An J.T. Conklin Aq Mt jtc@NetBSD.org .
2620f2a0901SsevanIt was rewritten again for
26352bf796fSjoerg.Nx 1.6
26452bf796fSjoergby
26503fb25d4Ssevan.An -nosplit
26603fb25d4Ssevan.An Jaromir Dolecek Aq Mt jdolecek@NetBSD.org .
26752bf796fSjoerg.Sh NOTES
26852bf796fSjoergThe empty string
26952bf796fSjoerg.Do Dc
27052bf796fSjoergcannot be matched with the intuitive:
27152bf796fSjoerg.Bd -literal -offset indent
27252bf796fSjoergexpr '' : '$'
27352bf796fSjoerg.Ed
27452bf796fSjoerg.Pp
27552bf796fSjoergThe reason is that the returned number of matched characters (zero)
27652bf796fSjoergis indistinguishable from a failed match, so this returns failure.
27752bf796fSjoergTo match the empty string, use something like:
27852bf796fSjoerg.Bd -literal -offset indent
27952bf796fSjoergexpr x'' : 'x$'
28052bf796fSjoerg.Ed
281