Evaluate expressions

expr EXPRESSION expr OPTION

--help display help and exit

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

[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.

[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.**

[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.

[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.

[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;"

[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.

[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.