In this article, we will show you various useful ways of doing arithmetic’s in the Linux terminal. By the end of this article, you will learn basic different practical ways of doing mathematical calculations in the command line.
Let’s get started!
1. Using Bash Shell
The first and easiest way do basic math on the Linux CLI is a using double parenthesis. Here are some examples where we use values stored in variables:
$ ADD=$(( 1 + 2 )) $ echo $ADD $ MUL=$(( $ADD * 5 )) $ echo $MUL $ SUB=$(( $MUL - 5 )) $ echo $SUB $ DIV=$(( $SUB / 2 )) $ echo $DIV $ MOD=$(( $DIV % 2 )) $ echo $MOD
2. Using expr Command
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).
$ expr 3 + 5 $ expr 15 % 3 $ expr 5 \* 3 $ expr 5 – 3 $ expr 20 / 4
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:
$ expr 5 = 3 $ expr 5 = 5 $ expr 8 != 5 $ expr 8 \> 5 $ expr 8 \< 5 $ expr 8 \<= 5
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).
$ NUM=$(( 1 + 2)) $ echo $NUM $ NUM=$(expr $NUM + 2) $ echo $NUM
Let’s also look at how to find the length of a string using:
$ expr length "This is Tecmint.com"
For more information especially on the meaning of the above operators, see the expr man page:
$ man expr
3. Using bc Command
bc (Basic Calculator) is a command-line utility that provides all features you expect from a simple scientific or financial calculator. It is specifically useful for doing floating point math.
If bc command not installed, you can install it using:
$ sudo apt install bc #Debian/Ubuntu $ sudo yum install bc #RHEL/CentOS $ sudo dnf install bc #Fedora 22+
Once installed, you can run it in interactive mode or non-interactively by passing arguments to it – we will look at both case. To run it interactively, type the command bc on command prompt and start doing some math, as shown.
$ bc
The following examples show how to use bc non-interactively on the command-line.
$ echo '3+5' | bc $ echo '15 % 2' | bc $ echo '15 / 2' | bc $ echo '(6 * 2) - 5' | bc
The -l flag is used to the default scale (digits after the decimal point) to 20, for example:
$ echo '12/5 | bc' $ echo '12/5 | bc -l'
4. Using Awk Command
Awk is one of the most prominent text-processing programs in GNU/Linux. It supports the addition, subtraction, multiplication, division, and modulus arithmetic operators. It is also useful for doing floating point math.
You can use it to do basic math as shown.
$ awk 'BEGIN { a = 6; b = 2; print "(a + b) = ", (a + b) }' $ awk 'BEGIN { a = 6; b = 2; print "(a - b) = ", (a - b) }' $ awk 'BEGIN { a = 6; b = 2; print "(a * b) = ", (a * b) }' $ awk 'BEGIN { a = 6; b = 2; print "(a / b) = ", (a / b) }' $ awk 'BEGIN { a = 6; b = 2; print "(a % b) = ", (a % b) }'
If you are new to Awk, we have a complete series of guides to get you started with learning it: Learn Awk Text Processing Tool.
5. Using factor Command
The factor command is use to decompose an integer into prime factors. For example:
$ factor 10 $ factor 127 $ factor 222 $ factor 110
That’s all! In this article, we have explained various useful ways of doing arithmetic’s in the Linux terminal. Feel free to ask any questions or share any thoughts about this article via the feedback form below.
Tags:
linux