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:
|
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:
|
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:
|
From the above example we can see how we can make use of expr on variables.We can store the result of the computed expression in another variable and carry out further processing as per our needs. The technique of storing the result in the variable using backticks is termed as command substitution and is not just limited to expr. Using backticks, we can assign the output of any command to a variable.
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:
|
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:
|
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:
|
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