The expr command evaluates expressions and prints the value of provided expression to standard output. We will look at different ways of using expr for doing simple math, making comparison, incrementing the value of a variable and finding the length of a string.
The following are some examples of doing simple calculations using the expr command. Note that many operators need to be escaped or quoted for shells, for instance the
* operator (we will look at more under comparison of expressions).
Next, we will cover how to make comparisons. When an expression evaluates to false, expr will print a value of 0, otherwise it prints 1.
Let’s look at some examples:
You can also use the expr command to increment the value of a variable. Take a look at the following example (in the same way, you can also decrease the value of a variable).
Let’s also look at how to find the length of a string using:
For more information especially on the meaning of the above operators, see the expr man page.