Can you solve the below math expression on your own without using any device? Take as much time as you need – but no tools allowed:
Kudos to you if you solved it on your own. I'm sure you'll be angry with me, though, after discovering that the result of this long expression just evaluated to zero.
So what if you used your browser to find the answer? How long would that take?
Searching this expression on Google took less than a second to get the answer (using a 500MBPS connection) along with 5,21,00,000 results.
Yahoo gave us the answer in less than 500 ms along with 1,480,000,000 search results.
Bing didn't even return anything.
But my terminal and system calculator were almost instant in finding the answer (took < 10 ms).
Using the terminal is one of the quickest ways to evaluate a mathematical expression. But many developers are unaware that you can solve mathematical expressions using your terminal.
In this article, you'll learn how to solve math problems in the Linux terminal.
How to Evaluate a Mathematical Expression in the Linux Terminal
You can use the
expr command to evaluate mathematical expression in your terminal. Basic mathematical operations such as addition, subtraction, multiplication, division and modulus all work using the
Let's have a quick look at each of these operations:
Running each of those commands in the terminal evaluates and returns the answer of that expression (as you can verify in the attached screenshot).
You might be thinking, "Why is there a backward slash (\) for the last 3 operations (Multiplication, Division, and Modulus)?"
A backward slash in programming is basically used to escape characters. Let's take the example of the Multiplication symbol (*). "*" is used in regular expressions which basically means to include all files and folders.
Here's a quick example:
Running the above command will copy all the files and folders in the current directory to the backup directory.
Similarly, each symbol has their own meaning. Using a backward slash (\) will escape its regular usage pattern. This is the reason that most symbols are prefixed with a backward slash.
But, you may wonder, is this all this command can do?
The answer is no. I've shared a sample of the expressions, but we could use any others and get the results in a fraction of a second.
How to Evaluate a Logical Expression in the Terminal
In addition to finding the results of a mathematical expression, you can use this command to evaluate a logical expression.
Logical expressions containing <, <=, >, >=, =, != can be evaluated with this command.
Let's have a quick look at how it works:
The command returns "1" if the expression evaluates to true and "0" if it evaluates to false.
From the above commands you can notice that all the logical operators are escaped with a backward slash (\).
What Do the Special Logical Operators Do?
I read your mind. You're thinking, "I know about logical operators. What are the special logical operators?".
Well, the special logical operators are:
- AND (&)
- OR (|)
From a programming perspective, the AND operator evaluates to true if both sides of the operator evaluates to true. The OR operator evaluates to true if either of the sides evaluates to true.
Here's the table for your reference. Some people may be familiar with
0. For those people, replace FALSE with
0 and TRUE with
|X||Y||X OR Y||X AND Y|
But they have a completely different usage with the
expr command. Let's have a quick look at it.
expr command works similar to the above table. But there's an exception for a particular case. This command will never return
1 if an expression evaluates to true – instead it returns an argument.
expr ARG1 | ARG2 returns ARG1 if ARG1 is neither
0, otherwise it'll return ARG2. On the other hand, evaluating
expr ARG1 & ARG2 returns ARG1 if neither of the arguments are
0 . It returns
Let's examine few examples with the logical OR operator:
The above screenshot represents operations with logical OR operator (|). Reading that you may still have a couple of questions:
What's the purpose of using empty double quotes ("") in the expression?
Empty double quotes represent a
null value in bash. So, as said earlier, if the first argument is null, the second argument is returned.
What if both the arguments are null?
When evaluating both arguments with null values, both the Logical OR and AND operators return
Let's examine a few examples with the logical AND operator:
How to Perform String Operations with the
expr command is not limited to mathematical and logical operations. It can perform string operations, too.
Pattern Matching with Regex
expr command can verify if a text matches with a regex pattern. It returns the matched pattern of the text if it exists or else returns an empty line.
The syntax to verify the regex is:
You can achieve this either by using the
match keyword or by using a semicolon (
:) between the string and the regex pattern.
Alternatively, you can use the semicolon to find the number of characters matching between the two texts.
Here's an example:
Find the length of a text
You can use the
expr length command to find the length of the given text.
You can enter the text directly after the
expr length command if it does not have any spaces. If the text has spaces, enclose them within double quotes (""), or else you'll get an error.
Find a character in a text
You can find a particular character from a given text using the
expr index command. It returns the place where the character exists.
Remember that the
expr index command performs a case-sensitive search and returns the first matching index of a character from the text.
Extract a substring from a string
Extracting a substring from a string is simple using
expr command. Entering the string, start index, and number of characters to be clipped after the start index will return the expected substring.
In this article, you have learnt the use cases of the
If you enjoyed my tutorial, you can subscribe to my newsletter on my personal site to receive more such insightful articles straight to your inbox. You'll also find a consolidated list of all my blog posts.