{"id":5886,"date":"2022-12-20T18:57:41","date_gmt":"2022-12-20T21:57:41","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/expr-man1p\/"},"modified":"2022-12-20T18:57:41","modified_gmt":"2022-12-20T21:57:41","slug":"expr-man1p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/expr-man1p\/","title":{"rendered":"EXPR (man1p)"},"content":{"rendered":"<h1 align=\"center\">EXPR<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#OPTIONS\">OPTIONS<\/a><br \/> <a href=\"#OPERANDS\">OPERANDS<\/a><br \/> <a href=\"#STDIN\">STDIN<\/a><br \/> <a href=\"#INPUT FILES\">INPUT FILES<\/a><br \/> <a href=\"#ENVIRONMENT VARIABLES\">ENVIRONMENT VARIABLES<\/a><br \/> <a href=\"#ASYNCHRONOUS EVENTS\">ASYNCHRONOUS EVENTS<\/a><br \/> <a href=\"#STDOUT\">STDOUT<\/a><br \/> <a href=\"#STDERR\">STDERR<\/a><br \/> <a href=\"#OUTPUT FILES\">OUTPUT FILES<\/a><br \/> <a href=\"#EXTENDED DESCRIPTION\">EXTENDED DESCRIPTION<\/a><br \/> <a href=\"#EXIT STATUS\">EXIT STATUS<\/a><br \/> <a href=\"#CONSEQUENCES OF ERRORS\">CONSEQUENCES OF ERRORS<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">expr \u2014 evaluate arguments as an expression<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">expr <i>operand<\/i>&#8230;<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>expr<\/i> utility shall evaluate an expression and write the result to standard output.<\/p>\n<h2>OPTIONS <a name=\"OPTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>OPERANDS <a name=\"OPERANDS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The single expression evaluated by <i>expr<\/i> shall be formed from the <i>operand<\/i> operands, as described in the EXTENDED DESCRIPTION section. The application shall ensure that each of the expression operator symbols:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">( ) | &#038; = > >= < <= != + - bodies manpages.csv script_extrae_body.sh script.sh usr \/ % :<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">and the symbols <i>integer<\/i> and <i>string<\/i> in the table are provided as separate arguments to <i>expr<\/i>.<\/p>\n<h2>STDIN <a name=\"STDIN\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Not used.<\/p>\n<h2>INPUT FILES <a name=\"INPUT FILES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>ENVIRONMENT VARIABLES <a name=\"ENVIRONMENT VARIABLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following environment variables shall affect the execution of <i>expr<\/i>:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><i>LANG<\/i><\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"74%\">\n<p>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2017, <i>Section 8.2<\/i>, <i>Internationalization Variables<\/i> for the precedence of internationalization variables used to determine the values of locale categories.)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><i>LC_ALL<\/i><\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"74%\">\n<p>If set to a non-empty string value, override the values of all the other internationalization variables.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>LC_COLLATE<\/i><\/p>\n<p style=\"margin-left:26%;\">Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"12%\">\n<p><i>LC_CTYPE<\/i><\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>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.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>LC_MESSAGES<\/i><\/p>\n<p style=\"margin-left:26%;\">Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"11%\">\n<p><i>NLSPATH<\/i><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"74%\">\n<p>Determine the location of message catalogs for the processing of <i>LC_MESSAGES<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>ASYNCHRONOUS EVENTS <a name=\"ASYNCHRONOUS EVENTS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Default.<\/p>\n<h2>STDOUT <a name=\"STDOUT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>expr<\/i> utility shall evaluate the expression and write the result, followed by a <newline>, to standard output.<\/p>\n<h2>STDERR <a name=\"STDERR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The standard error shall be used only for diagnostic messages.<\/p>\n<h2>OUTPUT FILES <a name=\"OUTPUT FILES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>EXTENDED DESCRIPTION <a name=\"EXTENDED DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 decreasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be left-associative.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-878271.png\" alt=\"Image grohtml-878271.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Matching Expression<\/b> <br \/> The <b>\u2019:\u2019<\/b> 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 POSIX.1-2017, <i>Section 9.3<\/i>, <i>Basic Regular Expressions<\/i>, 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 <b>\u2019^\u2019<\/b> is a special character in that context. Usually, the matching operator shall return a string representing the number of characters matched (<b>\u20190\u2019<\/b> on failure). Alternatively, if the pattern contains at least one regular expression subexpression <b>&#8220;[(&#8230;)]&#8221;<\/b>, the string matched by the back-reference expression <b>&#8220;1&#8221;<\/b> shall be returned. If the back-reference expression <b>&#8220;1&#8221;<\/b> does not match, then the null string shall be returned.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Identification as Integer or String<\/b> <br \/> An argument or the value of a subexpression that consists only of an optional unary minus followed by digits is a candidate for treatment as an integer if it is used as the left argument to the <i>|<\/i> operator or as either argument to any of the following operators: <i>&#038; = > >= < <= != + - bodies manpages.csv script_extrae_body.sh script.sh usr \/ %<\/i>. Otherwise, the argument or subexpression value shall be treated as a string.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The use of string arguments <b>length<\/b>, <b>substr<\/b>, <b>index<\/b>, or <b>match<\/b> produces unspecified results.<\/p>\n<h2>EXIT STATUS <a name=\"EXIT STATUS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following exit values shall be returned:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">0<\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"77%\">\n<p style=\"margin-top: 1em\">The <i>expression<\/i> evaluates to neither null nor zero.<\/p>\n<\/td>\n<td width=\"3%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>1<\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"77%\">\n<p>The <i>expression<\/i> evaluates to null or zero.<\/p>\n<\/td>\n<td width=\"3%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2<\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"77%\">\n<p>Invalid <i>expression<\/i>.<\/p>\n<\/td>\n<td width=\"3%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>>2<\/p>\n<\/td>\n<td width=\"6%\"><\/td>\n<td width=\"77%\">\n<p>An error occurred.<\/p>\n<\/td>\n<td width=\"3%\"> <\/td>\n<\/tr>\n<\/table>\n<h2>CONSEQUENCES OF ERRORS <a name=\"CONSEQUENCES OF ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Default.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>expr<\/i> utility has a rather difficult syntax:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>Each part of the expression is composed of separate arguments, so liberal usage of <blank> characters is required. For example:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-878272.png\" alt=\"Image grohtml-878272.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <i>Section 2.5<\/i>, <i>Parameters and Variables<\/i> and <i>Section 2.6.4<\/i>, <i>Arithmetic Expansion<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <b>&#8220;$a&#8221;<\/b> is <b>\u2019=\u2019<\/b>, the command:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr &#8220;$a&#8221; = &#8216;=&#8217;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">looks like:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr = = =<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">as the arguments are passed to <i>expr<\/i> (and they all may be taken as the <b>\u2019=\u2019<\/b> operator). The following works reliably:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr &#8220;X$a&#8221; = X=<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Also note that this volume of POSIX.1-2017 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 <b>&#8220;&#8211;&#8220;<\/b> construct of Guideline 10 of the Base Definitions volume of POSIX.1-2017, <i>Section 12.2<\/i>, <i>Utility Syntax Guidelines<\/i> 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>\n<p style=\"margin-left:11%; margin-top: 1em\">For testing string equality the <i>test<\/i> utility is preferred over <i>expr<\/i>, as it is usually implemented as a shell built-in. However, the functionality is not quite the same because the <i>expr =<\/i> and <i>!=<\/i> operators check whether strings collate equally, whereas <i>test<\/i> checks whether they are identical. Therefore, they can produce different results in locales where the collation sequence does not have a total ordering of all characters (see the Base Definitions volume of POSIX.1-2017, <i>Section 7.3.2<\/i>, <i>LC_COLLATE<\/i>).<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following command:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">a=$(expr &#8220;$a&#8221; + 1)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">adds 1 to the variable <i>a<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following command, for <b>&#8220;$a&#8221;<\/b> equal to either <b>\/usr\/abc\/file<\/b> or just <b>file<\/b>:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr $a : &#8216;.*\/(.*)&#8217; | $a<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">returns the last segment of a pathname (that is, <b>file<\/b>). Applications should avoid the character <b>\u2019\/\u2019<\/b> used alone as an argument; <i>expr<\/i> may interpret it as the division operator.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following command:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr &#8220;\/\/$a&#8221; : &#8216;.*\/(.*)&#8217;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is a better representation of the previous example. The addition of the <b>&#8220;\/\/&#8221;<\/b> 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 <b>&#8220;$a&#8221;<\/b> expand into multiple arguments.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following command:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">expr &#8220;X$VAR&#8221; : &#8216;.*&#8217; &#8211; 1<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">returns the number of characters in <i>VAR<\/i>.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historical applications.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<p style=\"margin-left:17%; margin-top: 1em\">expr foo : ^foo expr ^foo : ^foo<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Section 2.5<\/i>, <i>Parameters and Variables<\/i>, <i>Section 2.6.4<\/i>, <i>Arithmetic Expansion<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <i>Section 7.3.2<\/i>, <i>LC_COLLATE<\/i>, <i>Chapter 8<\/i>, <i>Environment Variables<\/i>, <i>Section 9.3<\/i>, <i>Basic Regular Expressions<\/i>, <i>Section 12.2<\/i>, <i>Utility Syntax Guidelines<\/i><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  expr \u2014 evaluate arguments as an expression <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3782,1],"tags":[2605,2511],"class_list":["post-5886","post","type-post","status-publish","format-standard","hentry","category-1p-posix-comandos-generales","category-sin-categoria","tag-expr","tag-man1p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5886"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5886\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}