expr

Evaluate expressions

Syntax

expr EXPRESSION
expr OPTION

Description

Print the value of EXPRESSION to standard output.

Options

--help display help and exit

Expressions

ARG1 | ARG2
      ARG1 if it is neither null nor 0, otherwise ARG2

ARG1 & ARG2
      ARG1 if neither argument is null or 0, otherwise 0

ARG1 < ARG2
      ARG1 is less than ARG2

ARG1 <= ARG2
      ARG1 is less than or equal to ARG2

ARG1 = ARG2
      ARG1 is equal to ARG2

ARG1 != ARG2
      ARG1 is unequal to ARG2

ARG1 >= ARG2
      ARG1 is greater than or equal to ARG2

ARG1 > ARG2
      ARG1 is greater than ARG2

ARG1 + ARG2
      arithmetic sum of ARG1 and ARG2

ARG1 - ARG2
      arithmetic difference of ARG1 and ARG2

ARG1 * ARG2
      arithmetic product of ARG1 and ARG2

ARG1 / ARG2
      arithmetic quotient of ARG1 divided by ARG2

ARG1 % ARG2
      arithmetic remainder of ARG1 divided by ARG2

STRING : REGEXP
      anchored pattern match of REGEXP in STRING

match STRING REGEXP
      same as STRING : REGEXP

substr STRING POS LENGTH
      substring of STRING, POS counted from 1

index STRING CHARS
      index in STRING where any CHARS is found, or 0

length STRING
      length of STRING

+ TOKEN
      interpret TOKEN as a string, even if it is a keyword like ‘match’ or an operator like ‘/’

( EXPRESSION )
      value of EXPRESSION

Example 1

Demonstrate simple mathematical operations in expr command.

[eleiss@eleiss ~]$ expr 10 + 10
20
[eleiss@eleiss ~]$ expr 10 / 10
1
[eleiss@eleiss ~]$ expr 10 - 10
0
[eleiss@eleiss ~]$ expr 10 \* 10
100
[eleiss@eleiss ~]$ expr 21 % 10
1
[eleiss@eleiss ~]$

The above example demonstrates how addition, subtraction, multiplication, division and modulation can be performed by expr command.

You can perform these operation by using the standard operator symbol. But while using multiplication operator, do not forget to escape the "*" otherwise it will throw a syntax error. This is so because the shell treat the symbol as a pattern matching character.

Example 2

Can expr command perform mathematical operations on float numbers ?

[eleiss@eleiss ~]$ expr 10 / 3
3
[eleiss@eleiss ~]$ expr 10.5 + 0.5
expr: non-numeric argument
[eleiss@eleiss ~]$

You can not perform operations on float numbers. As you can see in the output, the first command of division operation returns the floor value of the float number. The second command throws an error.

expr command treats float numbers as strings.

To perform floating point operations, one can use bc command.

Example 3

How can we perform logical expressions with expr command ?

[eleiss@eleiss ~]$ expr 10 \> 5
1
[eleiss@eleiss ~]$ expr 10 \> 25
0
[eleiss@eleiss ~]$ expr 10 \< 25
1
[eleiss@eleiss ~]$ expr 10 \< 2
0
[eleiss@eleiss ~]$ expr 10 \= 2
0
[eleiss@eleiss ~]$ expr 10 \= 10
1
[eleiss@eleiss ~]$ expr 10 \!= 10
0
[eleiss@eleiss ~]$ expr 10 \!= 20
1
[eleiss@eleiss ~]$

The output is self explanatory. When the expression is true, the command displays 0 or else it displays 1.

Do not forget to escape the operator symbols.

Example 4

How to match regular expression using expr command ?

[eleiss@eleiss ~]$ expr "I love Linux" : ^I.*love$
6
[eleiss@eleiss ~]$ expr "I love Linux" : ^I.*L.*$
12
[eleiss@eleiss ~]$ expr "I love Linux" : ^I.*do\ not.*$
0
[eleiss@eleiss ~]$ expr match "browser-v4.1-os" ^browser.*-os$
15
[eleiss@eleiss ~]$ expr match "browser-v4.1-os" ^rowser.*-os$
0
[eleiss@eleiss ~]$

If the string does not match the regular expression, the command returns 0.

Else the total number of characters that match the regular expression.

Example 5

Extract a part of string (substring) from a string using expr command.

[eleiss@eleiss ~]$ expr substr computer 2 5
omput
[eleiss@eleiss ~]$

The command extracts a substring starting at position 2 and length of 5 characters from the string "computer;"

Example 6

Calculate length of a string using expr command.

[eleiss@eleiss ~]$ expr length "Count the length of this string"
31
[eleiss@eleiss ~]$

expr prints the length of the given string when length is used with the command.

Example 7

Find the first position of a character in the given string using expr command.

[eleiss@eleiss ~]$ expr index "Where is n in this string ?" n
10
[eleiss@eleiss ~]$

expr prints the first position of a character in the given string when index is used with the command.


You may write to resources@eleiss.com for any Linux related queries.

advertisement