## On this page

# Bash Arithmetic Operations

Performing basic arithmetic operations such as addition, subtraction, multiplication, and division is essential for many scripting tasks. These operations can be used to increment a counter, calculate percentages or totals, or determine if a number is even or odd.

Bash provides several methods to carry out math calculations in your scripts. This article will cover the primary ways to perform arithmetic in Bash and demonstrate them with examples.

## The expr command

The `expr`

command evaluates an expression and prints the result. It is an external utility and not a Bash builtin.

The syntax is:

`expr argument1 operator argument2`

Here are some examples:

```
#!/bin/bash
expr 2 + 2
# Prints 4
expr 5 - 3
# Prints 2
expr \( 5 + 3 \) \* 2
# Prints 16
```

You can save the output to a variable using command substitution:

```
result=$( expr 15 - 2 )
echo $result #=> 13
```

Note that Bash's special characters such as the asterisk must be escaped when using expr to prevent misinterpretation. There must be spaces between the arguments and operator. If you use quotes, the expression will be printed as-is instead of being evaluated.

Here is an example:

```
#!/bin/bash
expr "6 + 3" #=> 6 + 3
# Quoted expressions are printed, not evaluated.
expr 3+2 #=> 3+2
# The asterisk has a special meaning in Bash, so we must escape it.
expr 5 \* 3
# Modulus operator demonstration
expr 15 % 2 #=> 1
```

The `expr`

command can be slow and cumbersome to use, as it is a binary rather than a shell builtin. It may fork a new process in a large for-loop, which is undesirable. Furthermore, the behavior of `expr`

may vary between systems. A better approach for arithmetic calculations is to use `let`

or double parentheses (arithmetic expansion), which we will cover next.

## The let Command

`let`

is a Bash builtin that evaluates arithmetic expressions. Its syntax is:

```
let result=expression
```

The expression can take several forms. The first part is always a variable into which the result is saved.

Here is an example:

```
let total=5+4
echo $total # Prints 9
```

With `let`

, no spaces are required around the arithmetic operations when not using quotes. However, quoting the expression improves readability.

```
let "total = 5 + 4"
echo $total # Prints 9
```

`let`

is useful for incrementing/decrementing variables:

```
let counter++
let counter--
```

And assigning the result of math operations to variables:

```
let "total *= 2" # Total = total * 2
let "value /= 4" # Value = value / 4
```

We can also use variables in expressions:

```
let "result = 1 + 30"
echo result # 30 + first command line argument
```

So `let`

provides a simple way of doing integer math in Bash scripts.

Like `let`

, you can also use the `declare`

builtin with the `-i`

option to do arithmetic operations:

```
declare -i x=5+3
echo $x # 8
```

## Arithmetic Expansion $((...))

The Arithmetic Expansion feature of the shell is the recommended way for evaluating arithmetic expressions in Bash. This built-in shell expansion allows you to perform math calculations using parentheses. Its syntax is:

```
$(( expression ))
```

Here are some examples:

```
val=$(( 10 + 5 )) # val = 15
(( count++ )) # Increment count
x=$(( a * b )) # x = a * b
```

You can space out without the need for quotations to improve readability. Variables within the expression don't need the $ prefix, which provides a clean format:

```
y=$(( x + 3 ))
```

Some advantages of this method:

- More readable than expr or let
- No need to escape * like in expr
- Supports increment ++ and decrement --
- Can combine variables and math intuitively

So arithmetic expansion is a powerful, versatile, and recommended approach for arithmetic in Bash scripts.

The square brackets `$[...]`

can also do Arithmetic Expansion in Bash, but this notation has been deprecated and should be avoided. Prefer the use of `$((...))`

instead. Here is an example of using square brackets to evaluate arithmetic operations:

```
val=$[1+5] # val = 6
[ $x -le 10 ] # Less than or equal to comparison
```

## Comparison of Arithmetic Methods

To summarize, here are the main ways to perform integer arithmetic in Bash:

`$((...))`

- arithmetic expansion, preferred method for robust and readable arithmetic operations in scripts.`let`

and`declare -i`

- useful for incrementing counters and variables`expr`

- external command, avoid it for math due to awkward syntax

## Bash Drawbacks

We have been performing arithmetic operations on integers (whole numbers) only up to now, and you may be wondering why. This is because Bash only supports integers and not floats or decimal numbers. As a result, the arithmetic operations we discussed here only apply to whole numbers. When you need more advanced math capabilities, you can utilize `bc`

(basic calculator). `bc`

is a command-line utility used for precision calculation in Linux and Bash scripts.

We will not discuss the usage of this utility as it is beyond the scope of this article.

Now you have a solid understanding of how to use Bash for basic math in your shell scripts. With these arithmetic tools, you can calculate totals, iterate through numbers, determine remainders, and more to support your scripting logic.

## Conclusion

Through this article, you have gained core skills in Bash arithmetic. You now understand the main methods - `expr`

, `let`

, and the preferred approach: arithmetic expansion `$((...))`

. You can now apply these learnings to increment counters, calculate totals or remainders, test numeric ranges, and other basic operations. This equips you to integrate math directly into scripts, avoiding external programs except when advanced functions like decimal numbers are required.