Untitled Document
expr Command

expr command evaluates arguments as an expression.

We mostly use this command when we do the mathematical compputations . We have used expr command in our shell examples - you can see some scripts at - Practice Examples . Lets see some more options and examples of expr command as discussed below .

1. Basic example -

Run the below expr command on your terminal .Returns 1 (true) if the expressions are equivalent, or 0 (false) if they are not .Here, the values 3 and 3 are equal, and therefore equivalent, so the output will be:

expr 3 = 3
Output - 1

2. Evaluating string -

Run the below expr command on your terminal . Here, two strings are being compared for equivalence. If the strings match exactly, character-for-character, the result will be 1 (true). Otherwise, the result will be 0. Here, the result is:

expr '3' = '3'
Output - 1

3. Multiplication of two integers-

In mathematical computation shell doesn't multiple two numbers lik num = $x*$y , you have to use expr or let command to perfom any mathematical operatiosn . Lets run the following command to multiply two numbers .

expr 5 * 5
Output - 25 

4. Division of two integers-

Here we will use / as a division operator . But / has a special meaning in Linux / Unix shell script so we have to suppress its special meaning by providing escape charater '\' . So our expr command for division looks like -

expr 20 \/ 5
Output - 4 

5. Incrementing counter -

Run the following the command and see the results -

count=2
count=`expr $count + 1`
Output - 3 
Untitled Document