logo

utils-std

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

expr.1 (9385B)


  1. .\" SPDX-License-Identifier: 0BSD
  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 October 5, 2016
  33. .Dt EXPR 1
  34. .Os
  35. .Sh NAME
  36. .Nm expr
  37. .Nd evaluate expression
  38. .Sh SYNOPSIS
  39. .Nm
  40. .Op Fl e
  41. .Ar expression
  42. .Sh DESCRIPTION
  43. The
  44. .Nm
  45. utility evaluates
  46. .Ar expression
  47. and writes the result on standard output.
  48. .Pp
  49. All operators and operands must be passed as separate arguments.
  50. Several of the operators have special meaning to command interpreters
  51. and must therefore be quoted appropriately.
  52. All integer operands are interpreted in base 10 and must consist of only
  53. an optional leading minus sign followed by one or more digits (unless
  54. less strict parsing has been enabled for backwards compatibility with
  55. prior versions of
  56. .Nm
  57. in
  58. .Fx ) .
  59. .Pp
  60. Arithmetic operations are performed using signed integer math with a
  61. range according to the C
  62. .Vt intmax_t
  63. data type (the largest signed integral type available).
  64. All conversions and operations are checked for overflow.
  65. Overflow results in program termination with an error message on stdout
  66. and with an error status.
  67. .Pp
  68. The
  69. .Fl e
  70. option enables backwards compatible behaviour as detailed below.
  71. .Pp
  72. Operators are listed below in order of increasing precedence; all
  73. are left-associative.
  74. Operators with equal precedence are grouped within symbols
  75. .Ql {
  76. and
  77. .Ql } .
  78. .Bl -tag -width indent
  79. .It Ar expr1 Li \&| Ar expr2
  80. Return the evaluation of
  81. .Ar expr1
  82. if it is neither an empty string nor zero;
  83. otherwise, returns the evaluation of
  84. .Ar expr2
  85. if it is not an empty string;
  86. otherwise, returns zero.
  87. .It Ar expr1 Li & Ar expr2
  88. Return the evaluation of
  89. .Ar expr1
  90. if neither expression evaluates to an empty string or zero;
  91. otherwise, returns zero.
  92. .It Ar expr1 Bro =, >, >=, <, <=, != Brc Ar expr2
  93. Return the results of integer comparison if both arguments are integers;
  94. otherwise, returns the results of string comparison using the locale-specific
  95. collation sequence.
  96. The result of each comparison is 1 if the specified relation is true,
  97. or 0 if the relation is false.
  98. .It Ar expr1 Bro +, - Brc Ar expr2
  99. Return the results of addition or subtraction of integer-valued arguments.
  100. .It Ar expr1 Bro *, /, % Brc Ar expr2
  101. Return the results of multiplication, integer division, or remainder of integer-valued arguments.
  102. .It Ar expr1 Li \&: Ar expr2
  103. The
  104. .Dq Li \&:
  105. operator matches
  106. .Ar expr1
  107. against
  108. .Ar expr2 ,
  109. which must be a basic regular expression.
  110. The regular expression is anchored
  111. to the beginning of the string with an implicit
  112. .Dq Li ^ .
  113. .Pp
  114. If the match succeeds and the pattern contains at least one regular
  115. expression subexpression
  116. .Dq Li "\e(...\e)" ,
  117. the string corresponding to
  118. .Dq Li \e1
  119. is returned;
  120. otherwise the matching operator returns the number of characters matched.
  121. If the match fails and the pattern contains a regular expression subexpression
  122. the null string is returned;
  123. otherwise 0.
  124. .El
  125. .Pp
  126. Parentheses are used for grouping in the usual manner.
  127. .Pp
  128. The
  129. .Nm
  130. utility makes no lexical distinction between arguments which may be
  131. operators and arguments which may be operands.
  132. An operand which is lexically identical to an operator will be considered a
  133. syntax error.
  134. See the examples below for a work-around.
  135. .Pp
  136. The syntax of the
  137. .Nm
  138. command in general is historic and inconvenient.
  139. New applications are advised to use shell arithmetic rather than
  140. .Nm .
  141. .Ss Compatibility with previous implementations
  142. Unless
  143. .Fx
  144. 4.x
  145. compatibility is enabled, this version of
  146. .Nm
  147. adheres to the
  148. .Tn POSIX
  149. Utility Syntax Guidelines, which require that a leading argument beginning
  150. with a minus sign be considered an option to the program.
  151. The standard
  152. .Fl Fl
  153. syntax may be used to prevent this interpretation.
  154. However, many historic implementations of
  155. .Nm ,
  156. including the one in previous versions of
  157. .Fx ,
  158. will not permit this syntax.
  159. See the examples below for portable ways to guarantee the correct
  160. interpretation.
  161. The
  162. .Xr check_utility_compat 3
  163. function (with a
  164. .Fa utility
  165. argument of
  166. .Dq Li expr )
  167. is used to determine whether backwards compatibility mode should be enabled.
  168. This feature is intended for use as a transition and debugging aid, when
  169. .Nm
  170. is used in complex scripts which cannot easily be recast to avoid the
  171. non-portable usage.
  172. Enabling backwards compatibility mode also implicitly enables the
  173. .Fl e
  174. option, since this matches the historic behavior of
  175. .Nm
  176. in
  177. .Fx . This option makes number parsing less strict and permits leading
  178. white space and an optional leading plus sign.
  179. In addition, empty operands
  180. have an implied value of zero in numeric context.
  181. For historical reasons, defining the environment variable
  182. .Ev EXPR_COMPAT
  183. also enables backwards compatibility mode.
  184. .Sh ENVIRONMENT
  185. .Bl -tag -width ".Ev EXPR_COMPAT"
  186. .It Ev EXPR_COMPAT
  187. If set, enables backwards compatibility mode.
  188. .El
  189. .Sh EXIT STATUS
  190. The
  191. .Nm
  192. utility exits with one of the following values:
  193. .Bl -tag -width indent -compact
  194. .It 0
  195. the expression is neither an empty string nor 0.
  196. .It 1
  197. the expression is an empty string or 0.
  198. .It 2
  199. the expression is invalid.
  200. .El
  201. .Sh EXAMPLES
  202. .Bl -bullet
  203. .It
  204. The following example (in
  205. .Xr sh 1
  206. syntax) adds one to the variable
  207. .Va a :
  208. .Dl "a=$(expr $a + 1)"
  209. .It
  210. This will fail if the value of
  211. .Va a
  212. is a negative number.
  213. To protect negative values of
  214. .Va a
  215. from being interpreted as options to the
  216. .Nm
  217. command, one might rearrange the expression:
  218. .Dl "a=$(expr 1 + $a)"
  219. .It
  220. More generally, parenthesize possibly-negative values:
  221. .Dl "a=$(expr \e( $a \e) + 1)"
  222. .It
  223. With shell arithmetic, no escaping is required:
  224. .Dl "a=$((a + 1))"
  225. .It
  226. This example prints the filename portion of a pathname stored
  227. in variable
  228. .Va a .
  229. Since
  230. .Va a
  231. might represent the path
  232. .Pa / ,
  233. it is necessary to prevent it from being interpreted as the division operator.
  234. The
  235. .Li //
  236. characters resolve this ambiguity.
  237. .Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
  238. .It
  239. With modern
  240. .Xr sh 1
  241. syntax,
  242. .Dl "\*q${a##*/}\*q"
  243. expands to the same value.
  244. .El
  245. .Pp
  246. The following examples output the number of characters in variable
  247. .Va a .
  248. Again, if
  249. .Va a
  250. might begin with a hyphen, it is necessary to prevent it from being
  251. interpreted as an option to
  252. .Nm ,
  253. and
  254. .Va a
  255. might be interpreted as an operator.
  256. .Bl -bullet
  257. .It
  258. To deal with all of this, a complicated command
  259. is required:
  260. .Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
  261. .It
  262. With modern
  263. .Xr sh 1
  264. syntax, this can be done much more easily:
  265. .Dl "${#a}"
  266. expands to the required number.
  267. .El
  268. .Sh SEE ALSO
  269. .Xr sh 1 ,
  270. .Xr test 1 ,
  271. .Xr check_utility_compat 3
  272. .Sh STANDARDS
  273. The
  274. .Nm
  275. utility conforms to
  276. .St -p1003.1-2008 ,
  277. provided that backwards compatibility mode is not enabled.
  278. .Pp
  279. Backwards compatibility mode performs less strict checks of numeric arguments:
  280. .Bl -bullet
  281. .It
  282. An empty operand string is interpreted as 0.
  283. .El
  284. .Bl -bullet
  285. .It
  286. Leading white space and/or a plus sign before an otherwise valid positive
  287. numeric operand are allowed and will be ignored.
  288. .El
  289. .Pp
  290. The extended arithmetic range and overflow checks do not conflict with
  291. POSIX's requirement that arithmetic be done using signed longs, since
  292. they only make a difference to the result in cases where using signed
  293. longs would give undefined behavior.
  294. .Pp
  295. According to the
  296. .Tn POSIX
  297. standard, the use of string arguments
  298. .Va length ,
  299. .Va substr ,
  300. .Va index ,
  301. or
  302. .Va match
  303. produces undefined results.
  304. In this version of
  305. .Nm ,
  306. these arguments are treated just as their respective string values.
  307. .Pp
  308. The
  309. .Fl e
  310. flag is an extension.
  311. .Sh HISTORY
  312. An
  313. .Nm
  314. utility first appeared in the Programmer's Workbench (PWB/UNIX).
  315. A public domain version of
  316. .Nm
  317. written by
  318. .An Pace Willisson Aq Mt pace@blitz.com
  319. appeared in
  320. .Bx 386 0.1 .
  321. .Sh AUTHORS
  322. Initial implementation by
  323. .An Pace Willisson Aq Mt pace@blitz.com
  324. was largely rewritten by
  325. .An -nosplit
  326. .An J.T. Conklin Aq Mt jtc@FreeBSD.org .