xref: /minix3/bin/expr/expr.1 (revision 7377a594e4aadf10d5bcbc0a70c8515160715364)
1*7377a594SSevan Janiyan.\"	$NetBSD: expr.1,v 1.36 2016/08/23 20:34:23 sevan Exp $
25debab0eSLionel Sambuc.\"
35debab0eSLionel Sambuc.\" Copyright (c) 2000,2003 The NetBSD Foundation, Inc.
45debab0eSLionel Sambuc.\" All rights reserved.
55debab0eSLionel Sambuc.\"
65debab0eSLionel Sambuc.\" This code is derived from software contributed to The NetBSD Foundation
75debab0eSLionel Sambuc.\" by J.T. Conklin <jtc@NetBSD.org> and Jaromir Dolecek <jdolecek@NetBSD.org>.
85debab0eSLionel Sambuc.\"
95debab0eSLionel Sambuc.\" Redistribution and use in source and binary forms, with or without
105debab0eSLionel Sambuc.\" modification, are permitted provided that the following conditions
115debab0eSLionel Sambuc.\" are met:
125debab0eSLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright
135debab0eSLionel Sambuc.\"    notice, this list of conditions and the following disclaimer.
145debab0eSLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright
155debab0eSLionel Sambuc.\"    notice, this list of conditions and the following disclaimer in the
165debab0eSLionel Sambuc.\"    documentation and/or other materials provided with the distribution.
175debab0eSLionel Sambuc.\"
185debab0eSLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
195debab0eSLionel Sambuc.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
205debab0eSLionel Sambuc.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
215debab0eSLionel Sambuc.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
225debab0eSLionel Sambuc.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
235debab0eSLionel Sambuc.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
245debab0eSLionel Sambuc.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
255debab0eSLionel Sambuc.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
265debab0eSLionel Sambuc.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
275debab0eSLionel Sambuc.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
285debab0eSLionel Sambuc.\" POSSIBILITY OF SUCH DAMAGE.
295debab0eSLionel Sambuc.\"
30*7377a594SSevan Janiyan.Dd August 23, 2016
315debab0eSLionel Sambuc.Dt EXPR 1
325debab0eSLionel Sambuc.Os
335debab0eSLionel Sambuc.Sh NAME
345debab0eSLionel Sambuc.Nm expr
355debab0eSLionel Sambuc.Nd evaluate expression
365debab0eSLionel Sambuc.Sh SYNOPSIS
375debab0eSLionel Sambuc.Nm
385debab0eSLionel Sambuc.Ar expression
395debab0eSLionel Sambuc.Sh DESCRIPTION
405debab0eSLionel SambucThe
415debab0eSLionel Sambuc.Nm
425debab0eSLionel Sambucutility evaluates
435debab0eSLionel Sambuc.Ar expression
445debab0eSLionel Sambucand writes the result on standard output.
455debab0eSLionel Sambuc.Pp
465debab0eSLionel SambucAll operators are separate arguments to the
475debab0eSLionel Sambuc.Nm
485debab0eSLionel Sambucutility.
495debab0eSLionel SambucCharacters special to the command interpreter must be escaped.
505debab0eSLionel Sambuc.Pp
515debab0eSLionel SambucOperators are listed below in order of increasing precedence.
525debab0eSLionel SambucOperators with equal precedence are grouped within { } symbols.
535debab0eSLionel Sambuc.Bl -tag -width indent
545debab0eSLionel Sambuc.It Ar expr1 Li \&| Ar expr2
555debab0eSLionel SambucReturns the evaluation of
565debab0eSLionel Sambuc.Ar expr1
575debab0eSLionel Sambucif it is neither an empty string nor zero;
585debab0eSLionel Sambucotherwise, returns the evaluation of
595debab0eSLionel Sambuc.Ar expr2 .
605debab0eSLionel Sambuc.It Ar expr1 Li \*[Am] Ar expr2
615debab0eSLionel SambucReturns the evaluation of
625debab0eSLionel Sambuc.Ar expr1
635debab0eSLionel Sambucif neither expression evaluates to an empty string or zero;
645debab0eSLionel Sambucotherwise, returns zero.
655debab0eSLionel Sambuc.It Ar expr1 Li "{=, \*[Gt], \*[Ge], \*[Lt], \*[Le], !=}" Ar expr2
665debab0eSLionel SambucReturns the results of integer comparison if both arguments are integers;
675debab0eSLionel Sambucotherwise, returns the results of string comparison using the locale-specific
685debab0eSLionel Sambuccollation sequence.
695debab0eSLionel SambucThe result of each comparison is 1 if the specified relation is true,
705debab0eSLionel Sambucor 0 if the relation is false.
715debab0eSLionel Sambuc.It Ar expr1 Li "{+, -}" Ar expr2
725debab0eSLionel SambucReturns the results of addition or subtraction of integer-valued arguments.
735debab0eSLionel Sambuc.It Ar expr1 Li "{*, /, %}" Ar expr2
745debab0eSLionel SambucReturns the results of multiplication, integer division, or remainder of integer-valued arguments.
755debab0eSLionel Sambuc.It Ar expr1 Li \&: Ar expr2
765debab0eSLionel SambucThe
775debab0eSLionel Sambuc.Dq \&:
785debab0eSLionel Sambucoperator matches
795debab0eSLionel Sambuc.Ar expr1
805debab0eSLionel Sambucagainst
815debab0eSLionel Sambuc.Ar expr2 ,
825debab0eSLionel Sambucwhich must be a regular expression.
835debab0eSLionel SambucThe regular expression is anchored
845debab0eSLionel Sambucto the beginning of the string with an implicit
855debab0eSLionel Sambuc.Dq ^ .
865debab0eSLionel Sambuc.Pp
875debab0eSLionel SambucIf the match succeeds and the pattern contains at least one regular
885debab0eSLionel Sambucexpression subexpression
895debab0eSLionel Sambuc.Dq "\e(...\e)" ,
905debab0eSLionel Sambucthe string corresponding to
915debab0eSLionel Sambuc.Dq "\e1"
925debab0eSLionel Sambucis returned;
935debab0eSLionel Sambucotherwise the matching operator returns the number of characters matched.
945debab0eSLionel SambucIf the match fails and the pattern contains a regular expression subexpression
955debab0eSLionel Sambucthe null string is returned;
965debab0eSLionel Sambucotherwise 0.
975debab0eSLionel Sambuc.It "( " Ar expr No " )"
985debab0eSLionel SambucParentheses are used for grouping in the usual manner.
995debab0eSLionel Sambuc.El
1005debab0eSLionel Sambuc.Pp
1015debab0eSLionel SambucAdditionally, the following keywords are recognized:
1025debab0eSLionel Sambuc.Bl -tag -width indent
1035debab0eSLionel Sambuc.It length Ar expr
1045debab0eSLionel SambucReturns the length of the specified string in bytes.
1055debab0eSLionel Sambuc.El
1065debab0eSLionel Sambuc.Pp
1075debab0eSLionel SambucOperator precedence (from highest to lowest):
1085debab0eSLionel Sambuc.Bl -enum -compact -offset indent
1095debab0eSLionel Sambuc.It
1105debab0eSLionel Sambucparentheses
1115debab0eSLionel Sambuc.It
1125debab0eSLionel Sambuclength
1135debab0eSLionel Sambuc.It
1145debab0eSLionel Sambuc.Dq \&:
1155debab0eSLionel Sambuc.It
1165debab0eSLionel Sambuc.Dq "*" ,
1175debab0eSLionel Sambuc.Dq "/" ,
1185debab0eSLionel Sambucand
1195debab0eSLionel Sambuc.Dq "%"
1205debab0eSLionel Sambuc.It
1215debab0eSLionel Sambuc.Dq "+"
1225debab0eSLionel Sambucand
1235debab0eSLionel Sambuc.Dq "-"
1245debab0eSLionel Sambuc.It
1255debab0eSLionel Sambuccompare operators
1265debab0eSLionel Sambuc.It
1275debab0eSLionel Sambuc.Dq \*[Am]
1285debab0eSLionel Sambuc.It
1295debab0eSLionel Sambuc.Dq \&|
1305debab0eSLionel Sambuc.El
1315debab0eSLionel Sambuc.Sh EXIT STATUS
1325debab0eSLionel SambucThe
1335debab0eSLionel Sambuc.Nm
1345debab0eSLionel Sambucutility exits with one of the following values:
1355debab0eSLionel Sambuc.Bl -tag -width Ds -compact
1365debab0eSLionel Sambuc.It 0
1375debab0eSLionel Sambucthe expression is neither an empty string nor 0.
1385debab0eSLionel Sambuc.It 1
1395debab0eSLionel Sambucthe expression is an empty string or 0.
1405debab0eSLionel Sambuc.It 2
1415debab0eSLionel Sambucthe expression is invalid.
1425debab0eSLionel Sambuc.It \*[Gt]2
1435debab0eSLionel Sambucan error occurred (such as memory allocation failure).
1445debab0eSLionel Sambuc.El
1455debab0eSLionel Sambuc.Sh EXAMPLES
1465debab0eSLionel Sambuc.Bl -enum
1475debab0eSLionel Sambuc.It
1485debab0eSLionel SambucThe following example adds one to variable
1495debab0eSLionel Sambuc.Dq a :
1505debab0eSLionel Sambuc.Dl a=`expr $a + 1`
1515debab0eSLionel Sambuc.It
1525debab0eSLionel SambucThe following example returns zero, due to subtraction having higher precedence
1535debab0eSLionel Sambucthan the
1545debab0eSLionel Sambuc.Dq \*[Am]
1555debab0eSLionel Sambucoperator:
1565debab0eSLionel Sambuc.Dl expr 1 '\*[Am]' 1 - 1
1575debab0eSLionel Sambuc.It
1585debab0eSLionel SambucThe following example returns the filename portion of a pathname stored
1595debab0eSLionel Sambucin variable
1605debab0eSLionel Sambuc.Dq a :
1615debab0eSLionel Sambuc.Dl expr "/$a" Li : '.*/\e(.*\e)'
1625debab0eSLionel Sambuc.It
1635debab0eSLionel SambucThe following example returns the number of characters in variable
1645debab0eSLionel Sambuc.Dq a :
1655debab0eSLionel Sambuc.Dl expr $a Li : '.*'
1665debab0eSLionel Sambuc.El
1675debab0eSLionel Sambuc.Sh COMPATIBILITY
1685debab0eSLionel SambucThis implementation of
1695debab0eSLionel Sambuc.Nm
1705debab0eSLionel Sambucinternally uses 64 bit representation of integers and checks for
1715debab0eSLionel Sambucover- and underflows.
1725debab0eSLionel SambucIt also treats
1735debab0eSLionel Sambuc.Dq /
1745debab0eSLionel Sambuc(the division mark) and option
1755debab0eSLionel Sambuc.Dq --
1765debab0eSLionel Sambuccorrectly depending upon context.
1775debab0eSLionel Sambuc.Pp
1785debab0eSLionel Sambuc.Nm
1795debab0eSLionel Sambucon other systems (including
1805debab0eSLionel Sambuc.Nx
1815debab0eSLionel Sambucup to and including
1825debab0eSLionel Sambuc.Nx 1.5 )
1835debab0eSLionel Sambucmight not be so graceful.
1845debab0eSLionel SambucArithmetic results might be arbitrarily
1855debab0eSLionel Sambuclimited on such systems, most commonly to 32 bit quantities.
1865debab0eSLionel SambucThis means such
1875debab0eSLionel Sambuc.Nm
1885debab0eSLionel Sambuccan only process values between -2147483648 and +2147483647.
1895debab0eSLionel Sambuc.Pp
1905debab0eSLionel SambucOn other systems,
1915debab0eSLionel Sambuc.Nm
1925debab0eSLionel Sambucmight also not work correctly for regular expressions where
1935debab0eSLionel Sambuceither side contains
1945debab0eSLionel Sambuc.Dq /
1955debab0eSLionel Sambuc(a single forward slash), like this:
1965debab0eSLionel Sambuc.Bd -literal -offset indent
1975debab0eSLionel Sambucexpr / : '.*/\e(.*\e)'
1985debab0eSLionel Sambuc.Ed
1995debab0eSLionel Sambuc.Pp
2005debab0eSLionel SambucIf this is the case, you might use
2015debab0eSLionel Sambuc.Dq //
2025debab0eSLionel Sambuc(a double forward slash)
2035debab0eSLionel Sambucto avoid confusion with the division operator:
2045debab0eSLionel Sambuc.Bd -literal -offset indent
2055debab0eSLionel Sambucexpr "//$a" : '.*/\e(.*\e)'
2065debab0eSLionel Sambuc.Ed
2075debab0eSLionel Sambuc.Pp
2085debab0eSLionel SambucAccording to
2095debab0eSLionel Sambuc.St -p1003.2 ,
2105debab0eSLionel Sambuc.Nm
2115debab0eSLionel Sambuchas to recognize special option
2125debab0eSLionel Sambuc.Dq -- ,
2135debab0eSLionel Sambuctreat it as a delimiter to mark the end of command
2145debab0eSLionel Sambucline options, and ignore it.
2155debab0eSLionel SambucSome
2165debab0eSLionel Sambuc.Nm
217*7377a594SSevan Janiyanimplementations do not recognize it at all; others
2185debab0eSLionel Sambucmight ignore it even in cases where doing so results in syntax
2195debab0eSLionel Sambucerror.
2205debab0eSLionel SambucThere should be same result for both following examples,
2215debab0eSLionel Sambucbut it might not always be:
2225debab0eSLionel Sambuc.Bl -enum -compact -offset indent
2235debab0eSLionel Sambuc.It
2245debab0eSLionel Sambucexpr -- : .
2255debab0eSLionel Sambuc.It
2265debab0eSLionel Sambucexpr -- -- : .
2275debab0eSLionel Sambuc.El
2285debab0eSLionel SambucAlthough
2295debab0eSLionel Sambuc.Nx
2305debab0eSLionel Sambuc.Nm
2315debab0eSLionel Sambuchandles both cases correctly, you should not depend on this behavior
2325debab0eSLionel Sambucfor portability reasons and avoid passing a bare
2335debab0eSLionel Sambuc.Dq --
2345debab0eSLionel Sambucas the first
2355debab0eSLionel Sambucargument.
2365debab0eSLionel Sambuc.Sh STANDARDS
2375debab0eSLionel SambucThe
2385debab0eSLionel Sambuc.Nm
2395debab0eSLionel Sambucutility conforms to
2405debab0eSLionel Sambuc.St -p1003.2 .
2415debab0eSLionel SambucThe
2425debab0eSLionel Sambuc.Ar length
2435debab0eSLionel Sambuckeyword is an extension for compatibility with GNU
2445debab0eSLionel Sambuc.Nm .
245*7377a594SSevan Janiyan.Sh HISTORY
246*7377a594SSevan JaniyanAn
247*7377a594SSevan Janiyan.Nm
248*7377a594SSevan Janiyanutility first appeared in the Programmer's Workbench (PWB/UNIX).
249*7377a594SSevan JaniyanA public domain version of
250*7377a594SSevan Janiyan.Nm
251*7377a594SSevan Janiyanwritten by
252*7377a594SSevan Janiyan.An Pace Willisson
253*7377a594SSevan Janiyan.Aq pace@blitz.com
254*7377a594SSevan Janiyanappeared in
255*7377a594SSevan Janiyan.Bx 386 0.1 .
2565debab0eSLionel Sambuc.Sh AUTHORS
257*7377a594SSevan JaniyanInitial implementation by
258*7377a594SSevan Janiyan.An Pace Willisson Aq Mt pace@blitz.com
259*7377a594SSevan Janiyanwas largely rewritten by
260*7377a594SSevan Janiyan.An -nosplit
261*7377a594SSevan Janiyan.An J.T. Conklin Aq Mt jtc@NetBSD.org .
262*7377a594SSevan JaniyanIt was rewritten again for
2635debab0eSLionel Sambuc.Nx 1.6
2645debab0eSLionel Sambucby
265*7377a594SSevan Janiyan.An -nosplit
266*7377a594SSevan Janiyan.An Jaromir Dolecek Aq Mt jdolecek@NetBSD.org .
2675debab0eSLionel Sambuc.Sh NOTES
2685debab0eSLionel SambucThe empty string
2695debab0eSLionel Sambuc.Do Dc
2705debab0eSLionel Sambuccannot be matched with the intuitive:
2715debab0eSLionel Sambuc.Bd -literal -offset indent
2725debab0eSLionel Sambucexpr '' : '$'
2735debab0eSLionel Sambuc.Ed
2745debab0eSLionel Sambuc.Pp
2755debab0eSLionel SambucThe reason is that the returned number of matched characters (zero)
2765debab0eSLionel Sambucis indistinguishable from a failed match, so this returns failure.
2775debab0eSLionel SambucTo match the empty string, use something like:
2785debab0eSLionel Sambuc.Bd -literal -offset indent
2795debab0eSLionel Sambucexpr x'' : 'x$'
2805debab0eSLionel Sambuc.Ed
281