logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

expr.1 (7501B)


  1. .\" SPDX-License-Identifier: BSD-4-Clause
  2. .\" -*- nroff -*-
  3. .\"-
  4. .\" Copyright (c) 1993 Winning Strategies, Inc.
  5. .\" All rights reserved.
  6. .\"
  7. .\" Redistribution and use in source and binary forms, with or without
  8. .\" modification, are permitted provided that the following conditions
  9. .\" are met:
  10. .\" 1. Redistributions of source code must retain the above copyright
  11. .\" notice, this list of conditions and the following disclaimer.
  12. .\" 2. Redistributions in binary form must reproduce the above copyright
  13. .\" notice, this list of conditions and the following disclaimer in the
  14. .\" documentation and/or other materials provided with the distribution.
  15. .\" 3. All advertising materials mentioning features or use of this software
  16. .\" must display the following acknowledgement:
  17. .\" This product includes software developed by Winning Strategies, Inc.
  18. .\" 4. The name of the author may not be used to endorse or promote products
  19. .\" derived from this software without specific prior written permission
  20. .\"
  21. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. .\"
  32. .Dd January 3, 2026
  33. .Dt EXPR 1
  34. .Os
  35. .Sh NAME
  36. .Nm expr
  37. .Nd evaluate expression
  38. .Sh SYNOPSIS
  39. .Nm
  40. .Ar expression
  41. .Sh DESCRIPTION
  42. The
  43. .Nm
  44. utility evaluates
  45. .Ar expression
  46. and writes the result on standard output.
  47. .Pp
  48. All operators and operands must be passed as separate arguments.
  49. Several of the operators have special meaning to command interpreters
  50. and must therefore be quoted appropriately.
  51. All integer operands are interpreted in base 10 and must consist of only
  52. an optional leading minus sign followed by one or more digits (unless
  53. less strict parsing has been enabled for backwards compatibility with
  54. prior versions of
  55. .Nm
  56. in
  57. .Fx ) .
  58. .Pp
  59. Arithmetic operations are performed using signed integer math with a
  60. range according to the C
  61. .Vt intmax_t
  62. data type (the largest signed integral type available).
  63. All conversions and operations are checked for overflow.
  64. Overflow results in program termination with an error message on stdout
  65. and with an error status.
  66. .Pp
  67. Operators are listed below in order of increasing precedence; all
  68. are left-associative.
  69. Operators with equal precedence are grouped within symbols
  70. .Ql {
  71. and
  72. .Ql } .
  73. .Bl -tag -width indent
  74. .It Ar expr1 Li \&| Ar expr2
  75. Return the evaluation of
  76. .Ar expr1
  77. if it is neither an empty string nor zero;
  78. otherwise, returns the evaluation of
  79. .Ar expr2
  80. if it is not an empty string;
  81. otherwise, returns zero.
  82. .It Ar expr1 Li & Ar expr2
  83. Return the evaluation of
  84. .Ar expr1
  85. if neither expression evaluates to an empty string or zero;
  86. otherwise, returns zero.
  87. .It Ar expr1 Bro =, >, >=, <, <=, != Brc Ar expr2
  88. Return the results of integer comparison if both arguments are integers;
  89. otherwise, returns the results of string comparison using the locale-specific
  90. collation sequence.
  91. The result of each comparison is 1 if the specified relation is true,
  92. or 0 if the relation is false.
  93. .It Ar expr1 Bro +, - Brc Ar expr2
  94. Return the results of addition or subtraction of integer-valued arguments.
  95. .It Ar expr1 Bro *, /, % Brc Ar expr2
  96. Return the results of multiplication, integer division, or remainder of integer-valued arguments.
  97. .It Ar expr1 Li \&: Ar expr2
  98. The
  99. .Dq Li \&:
  100. operator matches
  101. .Ar expr1
  102. against
  103. .Ar expr2 ,
  104. which must be a basic regular expression.
  105. The regular expression is anchored
  106. to the beginning of the string with an implicit
  107. .Dq Li ^ .
  108. .Pp
  109. If the match succeeds and the pattern contains at least one regular
  110. expression subexpression
  111. .Dq Li "\e(...\e)" ,
  112. the string corresponding to
  113. .Dq Li \e1
  114. is returned;
  115. otherwise the matching operator returns the number of characters matched.
  116. If the match fails and the pattern contains a regular expression subexpression
  117. the null string is returned;
  118. otherwise 0.
  119. .El
  120. .Pp
  121. Parentheses are used for grouping in the usual manner.
  122. .Pp
  123. The
  124. .Nm
  125. utility makes no lexical distinction between arguments which may be
  126. operators and arguments which may be operands.
  127. An operand which is lexically identical to an operator will be considered a
  128. syntax error.
  129. See the examples below for a work-around.
  130. .Pp
  131. The syntax of the
  132. .Nm
  133. command in general is historic and inconvenient.
  134. New applications are advised to use shell arithmetic rather than
  135. .Nm .
  136. .Sh EXIT STATUS
  137. The
  138. .Nm
  139. utility exits with one of the following values:
  140. .Bl -tag -width indent -compact
  141. .It 0
  142. the expression is neither an empty string nor 0.
  143. .It 1
  144. the expression is an empty string or 0.
  145. .It 2
  146. the expression is invalid.
  147. .El
  148. .Sh EXAMPLES
  149. .Bl -bullet
  150. .It
  151. The following example (in
  152. .Xr sh 1
  153. syntax) adds one to the variable
  154. .Va a :
  155. .Dl "a=$(expr $a + 1)"
  156. .It
  157. This will fail if the value of
  158. .Va a
  159. is a negative number.
  160. To protect negative values of
  161. .Va a
  162. from being interpreted as options to the
  163. .Nm
  164. command, one might rearrange the expression:
  165. .Dl "a=$(expr 1 + $a)"
  166. .It
  167. More generally, parenthesize possibly-negative values:
  168. .Dl "a=$(expr \e( $a \e) + 1)"
  169. .It
  170. With shell arithmetic, no escaping is required:
  171. .Dl "a=$((a + 1))"
  172. .It
  173. This example prints the filename portion of a pathname stored
  174. in variable
  175. .Va a .
  176. Since
  177. .Va a
  178. might represent the path
  179. .Pa / ,
  180. it is necessary to prevent it from being interpreted as the division operator.
  181. The
  182. .Li //
  183. characters resolve this ambiguity.
  184. .Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
  185. .It
  186. With modern
  187. .Xr sh 1
  188. syntax,
  189. .Dl "\*q${a##*/}\*q"
  190. expands to the same value.
  191. .El
  192. .Pp
  193. The following examples output the number of characters in variable
  194. .Va a .
  195. Again, if
  196. .Va a
  197. might begin with a hyphen, it is necessary to prevent it from being
  198. interpreted as an option to
  199. .Nm ,
  200. and
  201. .Va a
  202. might be interpreted as an operator.
  203. .Bl -bullet
  204. .It
  205. To deal with all of this, a complicated command
  206. is required:
  207. .Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
  208. .It
  209. With modern
  210. .Xr sh 1
  211. syntax, this can be done much more easily:
  212. .Dl "${#a}"
  213. expands to the required number.
  214. .El
  215. .Sh SEE ALSO
  216. .Xr sh 1 ,
  217. .Xr test 1
  218. .Sh STANDARDS
  219. The
  220. .Nm
  221. utility should be compliant with the
  222. IEEE Std 1003.1-2024 (“POSIX.1”)
  223. specification.
  224. .Pp
  225. The extended arithmetic range and overflow checks do not conflict with
  226. POSIX's requirement that arithmetic be done using signed longs, since
  227. they only make a difference to the result in cases where using signed
  228. longs would give undefined behavior.
  229. .Pp
  230. According to the
  231. .Tn POSIX
  232. standard, the use of string arguments
  233. .Va length ,
  234. .Va substr ,
  235. .Va index ,
  236. or
  237. .Va match
  238. produces undefined results.
  239. In this version of
  240. .Nm ,
  241. these arguments are treated just as their respective string values.
  242. .Sh HISTORY
  243. An
  244. .Nm
  245. utility first appeared in the Programmer's Workbench (PWB/UNIX).
  246. A public domain version of
  247. .Nm
  248. written by
  249. .An Pace Willisson Aq Mt pace@blitz.com
  250. appeared in
  251. .Bx 386 0.1 .
  252. .Sh AUTHORS
  253. Initial implementation by
  254. .An Pace Willisson Aq Mt pace@blitz.com
  255. was largely rewritten by
  256. .An -nosplit
  257. .An J.T. Conklin Aq Mt jtc@FreeBSD.org .