Bash expr with Examples
As we know bash variables are of string data type, performing arithmetic operations aren’t straightforward. However, bash provides us with several utilities to carry out arithmetic evaluations. One such command line tool is expr. Using the expr command, you can evaluate any given expression and display the result on the standard output.
Prerequisites
A text editor
Access to a terminal
Bash expr
Bash expr is a simple command line tool. It instructs bash to evaluate expressions and display the output on the terminal. It is not just limited to integers, you can use the expr command on strings as well as regular expressions.
Here’s the syntax:
Where expression is the expression to be carried out.
How to use bash expr
In this section, we will understand the use of expr command to perform arithmetic, comparison, and logical operations. We will explore how expr can be used with strings and even regular expressions. We also dive in to learn about command substitution using backticks (`) and how expr acts on variables.
1. Basic arithmetic operations
One of the most common uses of expr command is to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulus.
Let’s create a script (exprScript1.sh) by opening your favorite editor and saving it with the following contents:
#!/bin/bashecho
"------------------WITHOUT USING EXPR TO PERFORM ADDITION------------------"
echo
40+2
echo
"------------------USING EXPR TO PERFORM ADDITION------------------"
expr
10 + 5 #Notice the spaces between the operands and the operator
echo
"------------------USING EXPR TO PERFORM SUBTRACTION------------------"
expr
10 - 5
echo
"------------------USING EXPR TO PERFORM MULTIPLICATION------------------"
expr
10 \* 5 #Escape multiplication operator
echo
"------------------USING EXPR TO PERFORM DIVISION------------------"
expr
10 / 5
echo
"------------------USING EXPR TO PERFORM MODULUS ------------------"
expr
10 % 5
From the above example we can see that without using the expr command, bash will treat the numbers as strings and will print the expression without evaluating it. However, when we use the expr utility, arithmetic functionalities can be performed. One interesting thing to note is that we cannot use the asterisk (*) symbol directly for multiplication because this symbol will behave as a regular expression. As a result, it is important to escape it.
2. Comparison operations
The comparison operators are:
i < j : i is less than j
i <= j : i is less than or equal to j
i = j : i is equal to j
i != j : i is unequal to j
i >= j : i is greater than or equal to j
i > j : i is greater than j
Example:
#!/bin/bashecho
"------------------USING EXPR TO CHECK IF EQUAL------------------"
expr
10 = 5 #Notice the spaces between the operands and the operator
echo
"------------------USING EXPR TO CHECK IF NOT EQUAL------------------"
expr
10 != 5
echo
"------------------USING EXPR TO CHECK GREATER THAN------------------"
expr
10 \> 5
echo
"------------------USING EXPR TO CHECK GREATER THAN EQUAL------------------"
expr
10 \>= 5
echo
"------------------USING EXPR TO CHECK LESS THAN ------------------"
expr
10 \< 5
echo
"------------------USING EXPR TO CHECK LESS THAN EQUAL------------------"
expr
10 \<= 5
The above example demonstrates the use of expr command to perform comparison. The spacing between the operands and the operator is important. Furthermore, escape characters are required when using expr with > or < symbol. As you can notice the output generated is a boolean, either 0 or 1. 0 represents false whereas 1 represents true.
3. Operations on variables
The expr command can be used on variables and the output can be displayed on the terminal. The result of the expr command can also be stored in another variable. This can be done by making use of backticks (`). Let’s have a look at this example
Example:
#!/bin/basha=4b=2c=6d=3echo
"------------------USING EXPR TO PERFORM ADDITION ON VARIABLES------------------"expr
$a + $b#Alternatively we can store the value in a variableresult=`expr
$c + $d`echo
$result
echo
"------------------USING EXPR TO PERFORM SUBTRACTION ON VARIABLES------------------"expr
$a - $b#Alternatively we can store the value in a variableresult=`expr
$c - $d`echo
$result
echo
"------------------USING EXPR TO PERFORM MULTIPLICATION ON VARIABLES------------------"expr
$a \* $b#Alternatively we can store the value in a variableresult=`expr
$c \* $d`echo
$result
echo
"------------------USING EXPR TO PERFORM DIVISION ON VARIABLES------------------"expr
$a / $b#Alternatively we can store the value in a variableresult=`expr
$c / $d`echo
$result
echo
"------------------USING EXPR TO PERFORM MODULUS ON VARIABLES------------------"expr
$a % $b#Alternatively we can store the value in a variableresult=`expr
$c % $d`echo
$result
4. Operations on strings
This command line utility can be used on strings as well. You can find the length of the string, match two strings and even get the substring.
Here’s an example:
#!/bin/bashvar=Englandvar2=Ice
echo
"------------------ USING EXPR TO FIND LENGTH------------------"
expr
length "Iceland"expr
length $var
echo
"------------------ USING EXPR TO FIND SUBSTRING ------------------"
expr
substr "Iceland"
2 3expr
substr $var 2 3
echo
"------------------ USING EXPR TO FIND MATCHING NUMBER OF CHARACTERS------------------"
expr
"Iceland"
: "Ice"expr
$var : $var2
echo
"------------------ USING EXPR ON AN INTEGER AND A STRING------------------"
expr
5 = "5"
As shown in the above example, expr can be used to evaluate strings. The interesting thing to note is when expr is used to compare a number and a string. Technically, a numeric 5 should not be equal to string 5. However, the expr returns 1, that is true. This is because expr reads the string, evaluates it and finds out that it is a number. As a result it uses the numeric value of that string to compare.
5. Operations on regular expressions
Regular expressions are sequences used to match character combinations in strings. A regex may comprise a sequence of characters, metacharacters and/ or operators. The expr tool works with regular expressions as well.
Example:
#!/bin/bash
expr
Poland : '.*'
#count
var="I am on version 3.1"expr
match "$var"
'.*\([0-9][.][0-9]\)'
Regular expressions can be used with expr command. You can also store the string in a variable and use it as shown.
The first operation prints the number of characters that matched. The regular expression after the colon is compared with the text. The regular expression '.*' represents any number of any character. The second operation matches a number (in the format x.y) from the string. This regex requires brackets, otherwise it will print the number of characters matched rather than the match.
6. Logical operations
We can use the expr command to perform logical operations like OR and AND. The output of the expr command is a boolean, 0 or 1. 1 and 0 are represented as true and false, respectively.
Example:
#!/bin/bashecho
"------------------ LOGICAL OPERATIONS ON NUMBERS------------------"expr
12 "<"
5 "&"
13 ">"
10expr
12 "<"
5 "&"
25 - 8 ">"
10
expr
12 "<"
5 "|"
13 ">"
10expr
12 "<"
5 "|"
25 - 8 ">"
10
echo
"------------------ LOGICAL OPERATIONS ON STRINGS AND NUMBERS------------------"expr
length "messi"
"<"
5 "&"
25 - 8 ">"
10expr
length "messi"
"<"
5 "|"
25 - 8 ">"
10
echo
"------------------ LOGICAL OPERATIONS ON STRINGS------------------"
expr
length "lionel"
"<"
5 "&"
length "messi"
"<"
5expr
length "lionel"
"<"
5 "|"
length "messi"
">"
10
Here, in this example we can see how logical operations can be performed using the expr tool. This command line utility first evaluates the expressions and then does the logical operations. The logical operations can be even carried out between strings, numbers and strings and numbers, too. The outcome of the evaluation is a boolean 0 or 1.
Conclusion
Takeaway points we learned:
Bash treats variables as strings so to process them as integers, bash provides a simple command called expr.
expr command evaluates the expression and prints the output on the screen.
expr can be applied to variables, strings and regular expressions.
The result of the expr command can be stored in a variable.
Last updated