How to Solve Math Equations in the Linux Terminal
Can you solve the below math expression on your own without using any device? Take as much time as you need
( ( 11 + 97 ) + ( 2 * 63 ) - ( 7 / 93 ) * ( 8 - 25 ) / ( 9 * 64 ) ) * ( ( 64 / 34 ) + ( 94 - 20 ) - ( 23 + 98 ) * ( 199 * 928 ) / ( 92 * 26 ) ) * ( ( ( 2 * 1 ) / 2 ) - 1 )
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 a mathematical expression in your terminal. Basic mathematical operations such as addition, subtraction, multiplication, division, and modulus all work using the expr
command.
Let’s have a quick look at each of these operations:
expr 12 + 8 # Addition
expr 20 - 10 # Subtraction
expr 5 \* 2 # Multiplication
expr 8 \/ 4 # Division
expr 5 \% 3 # Modulus
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:
cp ./* ../backup/
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:
expr 2 \< 1
expr 29320 \> 23820
expr 29320 \>= 29320
expr 29320 \< 29320
expr 29320 \<= 29320
expr 293202 \= 293203
expr 293202 \!= 293203
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 evaluate to true. The OR operator evaluates to true if either of the sides evaluates to true.
Here’s the table for you to quickly remember. Some people may be familiar with 1
and 0
. For those people, replace FALSE with 0
and TRUE with 1
.
But they have a completely different usage with the expr
command. Let's have a quick look at it.
expr
commands work similarly to the above table. But there's an exception for a case. This command will never return 1 if an expression evaluates to true instead it returns an argument.
Evaluating expr ARG1 | ARG2
, returns ARG1 if ARG1 is neither null
nor 0
, otherwise, it'll return ARG2. On the other hand, evaluating expr ARG1 & ARG2
returns ARG1 if neither of the arguments are null
or 0
. It returns 0
otherwise.
Let’s examine few examples with Logical OR operator,
expr 1 \| 2 # Returns first argument (1)
expr 0 \| 2 # Returns second argument (2) as first argument is 0
expr "" \| 2 # Returns second argument (2) as first argument is null (Empty string [""] is considered as null in terminal)
expr 100 \| 78 # Returns first argument 100
expr "" \| "" # Returns 0 as both arguments are null ("")
The above screenshot represents operations with logical OR operator (|). Reading that you may stuck with 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 operator returns 0
.
Let’s examine few examples with Logical AND operator,
expr 1 \& 2 # Returns first argument (1)
expr 1 \& 0 # Returns 0 as one (second) argument is 0
expr 0 \& 1 # Returns 0 as one (first) argument is 0
expr "" \& 1 # Returns 0 as one (first) argument is null ("")
expr 1 \& "" # Returns 0 as one (second) argument is null ("")
expr "" \| "" # Returns 0 as both arguments are null ("")
How to Perform String Operations with the expr
Command
The expr
command is not limited to mathematical and logical operations. It can perform string operations, too.
Pattern Matching with Regex
The 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:
expr STRING : REGEXP or
expr match STRING REGEXP
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:
expr Remember : Remo # Returns 0
expr Remember : Rem # Returns 3
Find the length of a text
You can use the expr length
command to find the length of the given text.
expr length Linux
expr length "Learning Linux is fun"
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.
expr index STRING CHAR
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.
expr substr Carpenter 4 3 # Returns pen
Conclusion
In this article, you have learnt the use cases of the expr
command.
Subscribe to my newsletter to receive more such insightful articles that get delivered straight to your inbox.
Have a look at my site which has a consolidated list of all my blogs.
Originally published at https://www.gogosoon.com on December 15, 2022.