# 5 Useful Ways to Do Arithmetic in Linux Terminal

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 ))
\$ 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
```

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)

### 20 thoughts on “5 Useful Ways to Do Arithmetic in Linux Terminal”

1. and i.e.. (for python)

```python
27**(1/3)
^d
```
2. I prefer:

```python
1+2
3*(7%2)-1
2**8
7//3
etc.
^D
```
3. Well this is a great article, options except bc is more inclined to developers. In most of the cases for UNIX Linux admin bc is the best friend. There is always multiple way to do single task ultimately result matters

Nice work.

Thanks for sharing.

• @RSYDW

Welcome, thanks for the feedback.

• @bluebat

Thanks for sharing, we’ll check it out.

4. It is nice, but a calculator (physical or software) seems much easier.

5. Awk is a special case since this is basically an interpreter of the awk language. Like that, you could also use perl, python, php, with oneliners.

Another basic calculator is let:

```# let total=20+5
```

Results in \$total being 25

• @Tim

Many thanks for sharing this, we are grateful.

6. Another way is using perl command:

```# perl -wlne 'print eval';
```

then do the calculations

```5*6*9
270
12/5
2.4
10/3
3.33333333333333
....
```
• @Ulenaers

Great, thanks for sharing this.

7. Great article! Thanks!

• @01101001b

Welcome, thanks for the feedback.

8. Why not PowerShell? Available on most OS and far easier than anything you have here