Files
oldlinux-files/Ref-docs/POSIX/susv3/utilities/expr.html
2024-02-19 00:21:47 -05:00

578 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>expr</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="expr"></a> <a name="tag_04_50"></a><!-- expr -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_04_50_01"></a>NAME</h4>
<blockquote>expr - evaluate arguments as an expression</blockquote>
<h4><a name="tag_04_50_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>expr</tt> <i>operand</i></code></p>
</blockquote>
<h4><a name="tag_04_50_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>expr</i> utility shall evaluate an expression and write the result to standard output.</p>
</blockquote>
<h4><a name="tag_04_50_04"></a>OPTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_50_05"></a>OPERANDS</h4>
<blockquote>
<p>The single expression evaluated by <i>expr</i> shall be formed from the operands, as described in the EXTENDED DESCRIPTION
section. The application shall ensure that each of the expression operator symbols:</p>
<pre>
<tt>( ) | &amp; = &gt; &gt;= &lt; &lt;= != + - * / % :
</tt>
</pre>
<p>and the symbols <i>integer</i> and <i>string</i> in the table are provided as separate arguments to <i>expr</i>.</p>
</blockquote>
<h4><a name="tag_04_50_06"></a>STDIN</h4>
<blockquote>
<p>Not used.</p>
</blockquote>
<h4><a name="tag_04_50_07"></a>INPUT FILES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_50_08"></a>ENVIRONMENT VARIABLES</h4>
<blockquote>
<p>The following environment variables shall affect the execution of <i>expr</i>:</p>
<dl compact>
<dt><i>LANG</i></dt>
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
<dt><i>LC_ALL</i></dt>
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
<dt><i>LC_COLLATE</i></dt>
<dd><br>
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular
expressions and by the string comparison operators.</dd>
<dt><i>LC_CTYPE</i></dt>
<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
opposed to multi-byte characters in arguments) and the behavior of character classes within regular expressions.</dd>
<dt><i>LC_MESSAGES</i></dt>
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
error.</dd>
<dt><i>NLSPATH</i></dt>
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
</dl>
</blockquote>
<h4><a name="tag_04_50_09"></a>ASYNCHRONOUS EVENTS</h4>
<blockquote>
<p>Default.</p>
</blockquote>
<h4><a name="tag_04_50_10"></a>STDOUT</h4>
<blockquote>
<p>The <i>expr</i> utility shall evaluate the expression and write the result, followed by a &lt;newline&gt;, to standard
output.</p>
</blockquote>
<h4><a name="tag_04_50_11"></a>STDERR</h4>
<blockquote>
<p>The standard error shall be used only for diagnostic messages.</p>
</blockquote>
<h4><a name="tag_04_50_12"></a>OUTPUT FILES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_50_13"></a>EXTENDED DESCRIPTION</h4>
<blockquote>
<p>The formation of the expression to be evaluated is shown in the following table. The symbols <i>expr</i>, <i>expr1</i>, and
<i>expr2</i> represent expressions formed from <i>integer</i> and <i>string</i> symbols and the expression operator symbols (all
separate arguments) by recursive application of the constructs described in the table. The expressions are listed in order of
increasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be
left-associative.</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><b>Expression</b></p>
</th>
<th align="center">
<p class="tent"><b>Description</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;|&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Returns the evaluation of <i>expr1</i> if it is neither null nor zero; otherwise, returns the evaluation of
<i>expr2</i> if it is not null; otherwise, zero.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;&amp;&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Returns the evaluation of <i>expr1</i> if neither expression evaluates to null or zero; otherwise, returns
zero.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">&nbsp;</p>
</td>
<td align="left">
<p class="tent">Returns the result of a decimal integer comparison if both arguments are integers; otherwise, returns the result of
a string comparison using the locale-specific collation sequence. The result of each comparison is 1 if the specified relationship
is true, or 0 if the relationship is false.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;=&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Equal.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;&gt;&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Greater than.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;&gt;=&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Greater than or equal.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;&lt;&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Less than.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;&lt;=&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Less than or equal.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;!=&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Not equal.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;+&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Addition of decimal integer-valued arguments.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;-&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Subtraction of decimal integer-valued arguments.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;*&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Multiplication of decimal integer-valued arguments.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;/&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Integer division of decimal integer-valued arguments, producing an integer result.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;%&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Remainder of integer division of decimal integer-valued arguments.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr1</i>&nbsp;:&nbsp;<i>expr2</i></p>
</td>
<td align="left">
<p class="tent">Matching expression; see below.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">(&nbsp;<i>expr</i>&nbsp;)</p>
</td>
<td align="left">
<p class="tent">Grouping symbols. Any expression can be placed within parentheses. Parentheses can be nested to a depth of
{EXPR_NEST_MAX}.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>integer</i></p>
</td>
<td align="left">
<p class="tent">An argument consisting only of an (optional) unary minus followed by digits.</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>string</i></p>
</td>
<td align="left">
<p class="tent">A string argument; see below.</p>
</td>
</tr>
</table>
</center>
<h5><a name="tag_04_50_13_01"></a>Matching Expression</h5>
<p>The <tt>':'</tt> matching operator shall compare the string resulting from the evaluation of <i>expr1</i> with the regular
expression pattern resulting from the evaluation of <i>expr2</i>. Regular expression syntax shall be that defined in the Base
Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap09.html#tag_09_03">Section 9.3, Basic Regular
Expressions</a>, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the
first character of a string are matched by the regular expression) and, therefore, it is unspecified whether <tt>'^'</tt> is a
special character in that context. Usually, the matching operator shall return a string representing the number of characters
matched ( <tt>'0'</tt> on failure). Alternatively, if the pattern contains at least one regular expression subexpression
<tt>"[\(...\)]"</tt> , the string corresponding to <tt>"\1"</tt> shall be returned.</p>
<h5><a name="tag_04_50_13_02"></a>String Operand</h5>
<p>A string argument is an argument that cannot be identified as an <i>integer</i> argument or as one of the expression operator
symbols shown in the OPERANDS section.</p>
<p>The use of string arguments <b>length</b>, <b>substr</b>, <b>index</b>, or <b>match</b> produces unspecified results.</p>
</blockquote>
<h4><a name="tag_04_50_14"></a>EXIT STATUS</h4>
<blockquote>
<p>The following exit values shall be returned:</p>
<dl compact>
<dt>&nbsp;0</dt>
<dd>The <i>expression</i> evaluates to neither null nor zero.</dd>
<dt>&nbsp;1</dt>
<dd>The <i>expression</i> evaluates to null or zero.</dd>
<dt>&nbsp;2</dt>
<dd>Invalid <i>expression</i>.</dd>
<dt>&gt;2</dt>
<dd>An error occurred.</dd>
</dl>
</blockquote>
<h4><a name="tag_04_50_15"></a>CONSEQUENCES OF ERRORS</h4>
<blockquote>
<p>Default.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_04_50_16"></a>APPLICATION USAGE</h4>
<blockquote>
<p>After argument processing by the shell, <i>expr</i> is not required to be able to tell the difference between an operator and an
operand except by the value. If <tt>"$a"</tt> is <tt>'='</tt> , the command:</p>
<pre>
<tt>expr $a = '='
</tt>
</pre>
<p>looks like:</p>
<pre>
<tt>expr = = =
</tt>
</pre>
<p>as the arguments are passed to <i>expr</i> (and they all may be taken as the <tt>'='</tt> operator). The following works
reliably:</p>
<pre>
<tt>expr X$a = X=
</tt>
</pre>
<p>Also note that this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 permits implementations to extend utilities. The <i>expr</i>
utility permits the integer arguments to be preceded with a unary minus. This means that an integer argument could look like an
option. Therefore, the conforming application must employ the <tt>"--"</tt> construct of Guideline 10 of the Base Definitions
volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax
Guidelines</a> to protect its operands if there is any chance the first operand might be a negative integer (or any string with a
leading minus).</p>
</blockquote>
<h4><a name="tag_04_50_17"></a>EXAMPLES</h4>
<blockquote>
<p>The <i>expr</i> utility has a rather difficult syntax:</p>
<ul>
<li>
<p>Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line.</p>
</li>
<li>
<p>Each part of the expression is composed of separate arguments, so liberal usage of &lt;blank&gt;s is required. For example:</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><b>Invalid</b></p>
</th>
<th align="center">
<p class="tent"><b>Valid</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr</i> 1+2</p>
</td>
<td align="left">
<p class="tent"><i>expr</i> 1 + 2</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr</i> "1 + 2"</p>
</td>
<td align="left">
<p class="tent"><i>expr</i> 1 + 2</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><i>expr</i> 1 + (2 * 3)</p>
</td>
<td align="left">
<p class="tent"><i>expr</i> 1 + \( 2 \* 3 \)</p>
</td>
</tr>
</table>
</center>
</li>
</ul>
<p>In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their
equivalents in <i>expr</i>. Newly written scripts should avoid <i>expr</i> in favor of the new features within the shell; see <a
href="xcu_chap02.html#tag_02_05"><i>Parameters and Variables</i></a> and <a href="xcu_chap02.html#tag_02_06_04"><i>Arithmetic
Expansion</i></a> .</p>
<p>The following command:</p>
<pre>
<tt>a=$(expr $a + 1)
</tt>
</pre>
<p>adds 1 to the variable <i>a</i>.</p>
<p>The following command, for <tt>"$a"</tt> equal to either <b>/usr/abc/file</b> or just <b>file</b>:</p>
<pre>
<tt>expr $a : '.*/\(.*\)' \| $a
</tt>
</pre>
<p>returns the last segment of a pathname (that is, <b>file</b>). Applications should avoid the character <tt>'/'</tt> used alone
as an argument; <i>expr</i> may interpret it as the division operator.</p>
<p>The following command:</p>
<pre>
<tt>expr "//$a" : '.*/\(.*\)'
</tt>
</pre>
<p>is a better representation of the previous example. The addition of the <tt>"//"</tt> characters eliminates any ambiguity about
the division operator and simplifies the whole expression. Also note that pathnames may contain characters contained in the
<i>IFS</i> variable and should be quoted to avoid having <tt>"$a"</tt> expand into multiple arguments.</p>
<p>The following command:</p>
<pre>
<tt>expr "$VAR" : '.*'
</tt>
</pre>
<p>returns the number of characters in <i>VAR</i>.</p>
</blockquote>
<h4><a name="tag_04_50_18"></a>RATIONALE</h4>
<blockquote>
<p>In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historical
applications.</p>
<p>The use of a leading circumflex in the BRE is unspecified because many historical implementations have treated it as a special
character, despite their system documentation. For example:</p>
<pre>
<tt>expr foo : ^foo expr ^foo : ^foo
</tt>
</pre>
<p>return 3 and 0, respectively, on those systems; their documentation would imply the reverse. Thus, the anchoring condition is
left unspecified to avoid breaking historical scripts relying on this undocumented feature.</p>
</blockquote>
<h4><a name="tag_04_50_19"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_50_20"></a>SEE ALSO</h4>
<blockquote>
<p><a href="xcu_chap02.html#tag_02_05"><i>Parameters and Variables</i></a> , <a href="xcu_chap02.html#tag_02_06_04"><i>Arithmetic
Expansion</i></a></p>
</blockquote>
<h4><a name="tag_04_50_21"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 2.</p>
</blockquote>
<h4><a name="tag_04_50_22"></a>Issue 5</h4>
<blockquote>
<p>The FUTURE DIRECTIONS section is added.</p>
</blockquote>
<h4><a name="tag_04_50_23"></a>Issue 6</h4>
<blockquote>
<p>The <i>expr</i> utility is aligned with the IEEE&nbsp;P1003.2b draft standard, to include resolution of IEEE PASC Interpretation
1003.2 #104.</p>
<p>The normative text is reworded to avoid use of the term &quot;must&quot; for application requirements.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>