{
    "mode": "info",
    "parameter": "expr",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/expr/json",
    "generated": "2026-07-27T08:55:27Z",
    "sections": {
        "File: coreutils.info,  Node: expr invocation,  Prev: test invocation,  Up: Conditions": {
            "content": "",
            "subsections": [
                {
                    "name": "16.4 'expr': Evaluate expressions",
                    "content": "'expr' evaluates an expression and writes the result on standard output.\nEach token of the expression must be a separate argument.\n\nOperands are either integers or strings.  Integers consist of one or\nmore decimal digits, with an optional leading '-'.  'expr' converts\nanything appearing in an operand position to an integer or a string\ndepending on the operation being applied to it.\n\nStrings are not quoted for 'expr' itself, though you may need to\nquote them to protect characters with special meaning to the shell,\ne.g., spaces.  However, regardless of whether it is quoted, a string\noperand should not be a parenthesis or any of 'expr''s operators like\n'+', so you cannot safely pass an arbitrary string '$str' to expr merely\nby quoting it to the shell.  One way to work around this is to use the\nGNU extension '+', (e.g., '+ \"$str\" = foo'); a more portable way is to\nuse '\" $str\"' and to adjust the rest of the expression to take the\nleading space into account (e.g., '\" $str\" = \" foo\"').\n\nYou should not pass a negative integer or a string with leading '-'\nas 'expr''s first argument, as it might be misinterpreted as an option;\nthis can be avoided by parenthesization.  Also, portable scripts should\nnot use a string operand that happens to take the form of an integer;\nthis can be worked around by inserting leading spaces as mentioned\nabove.\n\nOperators may be given as infix symbols or prefix keywords."
                }
            ]
        },
        "Parentheses may be used for grouping in the usual manner.  You must": {
            "content": "quote parentheses and many operators to avoid the shell evaluating them,\nhowever.\n\nWhen built with support for the GNU MP library, 'expr' uses\narbitrary-precision arithmetic; otherwise, it uses native arithmetic\ntypes and may fail due to arithmetic overflow.\n\nThe only options are '--help' and '--version'.  *Note Common\noptions::.  Options must precede operands.\n\nExit status:\n\n0 if the expression is neither null nor 0,\n1 if the expression is null or 0,\n2 if the expression is invalid,\n3 if an internal error occurred (e.g., arithmetic overflow).\n\n* Menu:\n\n* String expressions::          + : match substr index length\n* Numeric expressions::         + - * / %\n* Relations for expr::          | & < <= = == != >= >\n* Examples of expr::            Examples.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: String expressions,  Next: Numeric expressions,  Up: expr invocation": {
            "content": "'expr' supports pattern matching and other string operators.  These have\nhigher precedence than both the numeric and relational operators (in the\nnext sections).\n\n'STRING : REGEX'\nPerform pattern matching.  The arguments are converted to strings\nand the second is considered to be a (basic, a la GNU 'grep')\nregular expression, with a '^' implicitly prepended.  The first\nargument is then matched against this regular expression.\n\nIf the match succeeds and REGEX uses '\\(' and '\\)', the ':'\nexpression returns the part of STRING that matched the\nsubexpression; otherwise, it returns the number of characters\nmatched.\n\nIf the match fails, the ':' operator returns the null string if\n'\\(' and '\\)' are used in REGEX, otherwise 0.\n\nOnly the first '\\( ... \\)' pair is relevant to the return value;\nadditional pairs are meaningful only for grouping the regular\nexpression operators.\n\nIn the regular expression, '\\+', '\\?', and '\\|' are operators which\nrespectively match one or more, zero or one, or separate\nalternatives.  SunOS and other 'expr''s treat these as regular\ncharacters.  (POSIX allows either behavior.)  *Note Regular\nExpression Library: (regex)Top, for details of regular expression\nsyntax.  Some examples are in *note Examples of expr::.\n\n'match STRING REGEX'\nAn alternative way to do pattern matching.  This is the same as\n'STRING : REGEX'.\n\n'substr STRING POSITION LENGTH'\nReturns the substring of STRING beginning at POSITION with length\nat most LENGTH.  If either POSITION or LENGTH is negative, zero, or\nnon-numeric, returns the null string.\n\n'index STRING CHARSET'\nReturns the first position in STRING where the first character in\nCHARSET was found.  If no character in CHARSET is found in STRING,\nreturn 0.\n\n'length STRING'\nReturns the length of STRING.\n\n'+ TOKEN'\nInterpret TOKEN as a string, even if it is a keyword like MATCH or\nan operator like '/'.  This makes it possible to test 'expr length\n+ \"$x\"' or 'expr + \"$x\" : '.*/\\(.\\)'' and have it do the right\nthing even if the value of $X happens to be (for example) '/' or\n'index'.  This operator is a GNU extension.  Portable shell scripts\nshould use '\" $token\" : ' \\(.*\\)'' instead of '+ \"$token\"'.\n\nTo make 'expr' interpret keywords as strings, you must use the\n'quote' operator.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Numeric expressions,  Next: Relations for expr,  Prev: String expressions,  Up: expr invocation": {
            "content": "'expr' supports the usual numeric operators, in order of increasing\nprecedence.  These numeric operators have lower precedence than the\nstring operators described in the previous section, and higher\nprecedence than the connectives (next section).\n\n'+ -'\nAddition and subtraction.  Both arguments are converted to\nintegers; an error occurs if this cannot be done.\n\n'* / %'\nMultiplication, division, remainder.  Both arguments are converted\nto integers; an error occurs if this cannot be done.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Relations for expr,  Next: Examples of expr,  Prev: Numeric expressions,  Up: expr invocation": {
            "content": "'expr' supports the usual logical connectives and relations.  These have\nlower precedence than the string and numeric operators (previous\nsections).  Here is the list, lowest-precedence operator first.\n\n'|'\nReturns its first argument if that is neither null nor zero,\notherwise its second argument if it is neither null nor zero,\notherwise 0.  It does not evaluate its second argument if its first\nargument is neither null nor zero.\n\n'&'\nReturn its first argument if neither argument is null or zero,\notherwise 0.  It does not evaluate its second argument if its first\nargument is null or zero.\n\n'< <= = == != >= >'\nCompare the arguments and return 1 if the relation is true, 0\notherwise.  '==' is a synonym for '='.  'expr' first tries to\nconvert both arguments to integers and do a numeric comparison; if\neither conversion fails, it does a lexicographic comparison using\nthe character collating sequence specified by the 'LCCOLLATE'\nlocale.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Examples of expr,  Prev: Relations for expr,  Up: expr invocation": {
            "content": "Here are a few examples, including quoting for shell metacharacters.\n\nTo add 1 to the shell variable 'foo', in Bourne-compatible shells:\n\nfoo=$(expr $foo + 1)\n\nTo print the non-directory part of the file name stored in '$fname',\nwhich need not contain a '/':\n\nexpr $fname : '.*/\\(.*\\)' '|' $fname\n\nAn example showing that '\\+' is an operator:\n\nexpr aaa : 'a\\+'\n=> 3\n\nexpr abc : 'a\\(.\\)c'\n=> b\nexpr index abcdef cz\n=> 3\nexpr index index a\nerror-> expr: syntax error\nexpr index + index a\n=> 0\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}