# Arithmetics in Bash Script

Introduction | |

let | |

expr | |

Double Parentheses | |

Add | |

Mod | |

Remainder | |

bc: float add | |

bc: float divide | |

bc: float multiply | |

Related Articles |

## Introduction

There are many ways to perform arithmetic operations in bash. In this article you will find a detailed overview of the use of let, expr, (()) and bc

## Let

let is a built-in bash function that allows you to perform basic arithmetic operations.

It is used as follows:

let <arithmetic operation>

Let's look at the examples in the script
**
let_example.sh
**

`#!/bin/bash`

`# Simple arithmetic with let`

```
```

`let A=5+4`

`echo $A # 9`

```
```

`let "a = 5 + 4"`

`echo $A # 9`

```
```

`let a++`

`echo $A # 10`

```
```

`let "a = 4 * 5"`

`echo $A # 20`

```
```

`let "a = $1 + 30"`

`echo $A # 30 + первый аргумент из командной строки`

Let's analyze the commands in turn:

Line 4 is the simplest form of writing without quotes.

Remember that if the calculation goes without quotes, you cannot put spaces

Line 7 - If you put quotes, you can use a space for better readability.\

Line 10 - Increase the value by 1. This is an analogue of "a = a + 1".

Line 16 - Other variables can be included in the expression.

./let_example.sh 11

9

9

10

20

41

Table of basic operators

Operator | Operation |
---|---|

+, -, \*, / | Addition, subtraction, multiplication, division |

var++ | Increasing the variable by 1 |

var-- | Reducing the variable by 1 |

% | Module: returns the remainder of the division |

## expr

expr is similar to let except that instead of saving the result to a variable, expr prints the response by default.

But no one forbids saving the expr result to variables using command substitution: x = $(extra 2 + 2)

Unlike let, it is not necessary to enclose expressions with spaces in quotation marks

You need to put spaces around the operators.

expr item1 operator item2

Consider a simple example:

**
expr_example.sh
**

`#!/bin/bash`

`# Simple arithmetic using expr`

```
```

`expr 5 + 4`

```
```

`expr "5 + 4"`

```
```

`expr 5+4`

```
```

`expr 5 \* $1`

```
```

`expr 11 % 2`

```
```

`A=$( expr 10 - 3 )`

`echo $A # 7`

Let's break it down:

Line 4 is the basic syntax. Pay attention to the spaces and the absence of quotation marks.

Line 6 - If you enclose an expression in quotation marks, it will output it in this form to the terminal.

Line 8 - If you do not put spaces, the expression will be output to the terminal without calculation.

Line 10 - Some characters need to be escaped.

Line 12 is a division modulo. The result will be the remainder of the integer division of two numbers.

Line 14 is an example of executing command substitution to save the result to variable a.

./expr_example.sh 12

9

5 + 4

5+4

60

1

7

## Double brackets

The result of the command execution can be easily saved to a variable.

VAR=$(some kind of command)

Arithmetic operations can be performed based on this mechanism. It is enough to use two brackets instead of one pair.

$(( expression ))

Consider the examples in the script

**
expansion_example.sh
**

`#!/bin/bash`

`# Простая арифметика с двойными скобками`

```
```

`A=$(( 4 + 5 ))`

`echo $A # 9`

```
```

`A=$((3+5))`

`echo $A # 8`

```
```

`B=$(( A + 3 ))`

`echo $B # 11`

```
```

`B=$(( $A + 4 ))`

`echo $B # 12`

```
```

`(( B++ ))`

`echo $B # 13`

```
```

`(( B += 3 ))`

`echo $B # 16`

```
```

`A=$(( 4 * 5 ))`

`echo $A # 20`

Let's analyze this script:

Line 4 is the basic syntax. You can put spaces without using quotation marks.

Line 7 - Works without spaces.

Line 10 - You can use variables without $ in front of them.

Line 13 - And it is possible with $

Line 16 - Increment of the variable by 1. The symbol $ is not needed.

Line 19 is an increase of the variable by 3. This is the short form of the entry b = b + 3.

Line 19 - Unlike other methods, the * symbol does not need to be escaped.

./expansion_example.sh

9

8

11

12

13

16

20

Double brackets give quite a lot of freedom in formatting the code.

They are available in Bash by default and their efficiency is slightly higher. Although it will not be easy to notice the difference on modern computers.

## Addition

Addition in bash can be performed in the following ways

`#!/bin/bash`

`# Bash +`

```
```

`# let`

`A=2`

`B=3`

```
```

`let C=$A+$B`

`echo $C`

./add.sh

5

`#!/bin/bash`

`# Bash +`

```
```

`# expr and (( ))`

`D=4`

`E=5`

```
```

`F=$( expr $D + $E )`

`G=$(( $D + $D ))`

```
```

`echo $F`

`echo $G`

./add.sh

9

8

## Integer division

bash implements integer division by default

Consider what happens if you try to divide the numbers using let

`#!/bin/bash`

`# Integer division`

```
```

`A=100`

`B=3`

`let "C = $A / $B"`

`echo "C: $C"`

C: 33

The fractional part is lost

If you are not satisfied with this, use
bc

## The remainder of the division

The remainder of the division can be obtained using **%** in the following original way

a=6

b=$(($a%5))

echo $b

1

## Float Addition

Floating point addition can be performed using **bc**

`#!/bin/bash`

`# Bash +`

```
```

`# bc`

`X=1.5`

`Y=3.5`

```
```

`Z=$(bc<<<"scale=3;$X+$Y")`

`echo "1.5 + 3.5 = " $Z`

./add.sh

1.5 + 3.5 = 5.0

## bc

There is no native support for floating-point number division in bash. But there are utilities that can do this.

One of them is bc

What happens if you try to divide the numbers using let

`#!/bin/bash`

`# Деление с let`

```
```

`A=100`

`B=3`

`let "C = $A / $B"`

`echo "C: $C"`

C: 33

The fractional part is lost

To avoid this issue use bc

`# Деление с bc`

`D=$(bc<<<"scale=3;$A/$B")`

`echo "D: $D"`

D: 33.333

## bc: float multiplication

It is also convenient to multiply floating point numbers using bc

`#!/bin/bash`

`# Multiplication with bc`

```
```

`A=2.5`

`B=5.6`

`С=$(bc<<<"scale=3;$A*$B")`

`echo "С: $C"`

C: 14.00