fortypoundhead.com

EXPR

Posted On 2007-04-30 by FortyPoundHead
Keywords: Command Reference
Tags: Linux Commandline Linux
Views: 1449


SYNTAX
expr expression...

DESCRIPTION
Each token of the expression must be a separate argument.

Operands are either numbers or strings. `expr'' coerces anything
appearing in an operand position to an integer or a string depending on
the operation being applied to it.

Strings are not quoted for `expr'' itself, though you may need to
quote them to protect characters with special meaning to the shell,
e.g., spaces.

Operators may given as infix symbols or prefix keywords. Parentheses
may be used for grouping in the usual manner (you must quote parentheses
to avoid the shell evaluating them, however).

String expressions
------------------

`expr'' supports pattern matching and other string operators. These
have lower precedence than both the numeric and relational operators (in
the next sections).

`STRING : REGEX''
Perform pattern matching. The arguments are coerced to strings
and the second is considered to be a (basic, a la GNU `grep'')
regular expression, with a `^'' implicitly prepended. The first
argument is then matched against this regular expression.

If the match succeeds and REGEX uses `\('' and `\)'', the `:''
expression returns the part of STRING that matched the
subexpression; otherwise, it returns the number of characters
matched.

If the match fails, the `:'' operator returns the null string if
`\('' and `\)'' are used in REGEX, otherwise 0.

Only the first `\( ... \)'' pair is relevant to the return value;
additional pairs are meaningful only for grouping the regular
expression operators.

In the regular expression, `\+'', `\?'', and `\|'' are operators
which respectively match one or more, zero or one, or separate
alternatives. SunOS and other `expr''''s treat these as regular
characters. (POSIX allows either behavior.)

`match STRING REGEX''
An alternative way to do pattern matching. This is the same as
`STRING : REGEX''.

`substr STRING POSITION LENGTH''
Returns the substring of STRING beginning at POSITION with length
at most LENGTH. If either POSITION or LENGTH is negative, zero,
or non-numeric, returns the null string.

`index STRING CHARSET''
Returns the first position in STRING where the first character in
CHARSET was found. If no character in CHARSET is found in STRING,
return 0.

`length STRING''
Returns the length of STRING.

`quote TOKEN''
Interpret TOKEN as a string, even if it is a keyword like MATCH or
an operator like `/''. This makes it possible to test `expr length
quote "$x"'' or `expr quote "$x" : ''.*/\(.\)'''' and have it do the
right thing even if the value of $X happens to be (for example)
`/'' or `index''. This operator is a GNU extension. It is disabled
when the environment variable `POSIXLY_CORRECT'' is set.

To make `expr'' interpret keywords as strings, you must use the
`quote'' operator.

Numeric expressions
-------------------

`expr'' supports the usual numeric operators, in order of increasing
precedence. The string operators (previous section) have lower
precedence, the connectives (next section) have higher.

`+ -''
Addition and subtraction. Both arguments are coerced to numbers;
an error occurs if this cannot be done.

`* / %''
Multiplication, division, remainder. Both arguments are coerced to
numbers; an error occurs if this cannot be done.

Relations for `expr''
--------------------

`expr'' supports the usual logical connectives and relations. These
are higher precedence than either the string or numeric operators
(previous sections). Here is the list, lowest-precedence operator
first.

`|'' Returns its first argument if that is neither null nor 0,
otherwise its second argument.

`&'' Return its first argument if neither argument is null or 0,
otherwise 0.

`< <= = == != >= >''
Compare the arguments and return 1 if the relation is true, 0
otherwise. `=='' is a synonym for `=''. `expr'' first tries to
coerce both arguments to numbers and do a numeric comparison; if
either coercion fails, it does a lexicographic comparison.

Exit status:
------------

0 if the expression is neither null nor 0,
1 if the expression is null or 0,
2 for invalid expressions.

Examples

Here are a few examples, including quoting for shell metacharacters.

To add 1 to the shell variable `foo'', in Bourne-compatible shells:
foo=`expr $foo + 1`

To print the non-directory part of the file name stored in `$fname'',
which need not contain a `/''.
expr $fname : ''.*/\(^.*\)'' ''^|'' $fname

An example showing that `\+'' is an operator:
expr aaa : ''a\+''
=> 3

expr abc : ''a\(.\)c''
=> b
expr index abcdef cz
=> 3
expr index index a
error--> expr: syntax error
expr index quote index a
=> 0


About the Author

FortyPoundHead has posted a total of 1974 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.161.91.76

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers