Arithmetic in Bash

How to Add Calculations to a Bash Script

Kids working on a computer in a lab

 Caiaimage/Robert Daly / Getty Images

Although Bash is a scripting language, it has pretty much all the capabilities of a general purpose programming language. This includes arithmetic functions. There are a number of syntax options you can use to evoke arithmetic evaluation of an expression. Perhaps the most readable one is the let command. For example:

let m=(4 * 1024)

will compute 4 times 1024 and assign the result to the variable "m".

You can print out the result by adding an echo statement:

echo $m

You can also create a file containing the Bash commands, in which case you should add a line at the top of the file that specifies the program that is supposed to execute the code. For example:

#! /bin/bash

assuming the Bash executable is located in /bin/bash. You also need to set the permissions of your script file so that it is executable. Assuming the script file name is, you can set the permissions to make the file executable with the command:

chmod +x

After that you can execute it with the command:


The available arithmetic operations are similar to those in standard programming languages like Java and C. Besides multiplication, as illustrated above, you use addition:

let m=(5 + 5)

or subtraction:

let m=(10 - 2)

or division:

let m=(10/2)

or modulo (the remainder after an integer division):

let m=(11/2)

When an operation is applied to the same variable to which the result is assigned you can use the standard arithmetic shorthand assignment operators, also referred to as compound assignment operators. For example, for addition, we have:

(( m+=15 ))

which is equivalent to "m = m + 15". For subtraction we have:

(( m-=3 ))

which is equivalent to "m = m - 3". For division we have:

(( m/=5 ))

which is equivalent to "m = m / 5". And for modulo, we have:

(( m%=10 ))

which is equivalent to "m = m % 10".

Additionally, you can use the increment and decrement operators:

(( m++ ))

is equivalent to "m = m + 1". And

(( m-- ))

is equivalent to "m = m - 1".

Floating Point Arithmetic in Bash

The let operator only works for integer arithmetic. For floating point arithmetic you can use for example the GNU bc calculator as illustrated in this example:

echo 32.0+1.4 | bc

The "pipe" operator "|" passes the arithmetic expression "32.0 + 1.4" to the bc calculator, which returns the real number. The echo command prints the result to the standard output.

Alternative Syntax for Arithmetic

Backticks (back single quotes) can be used to evaluate an arithmetic expression as in this example:

echo `expr $m + 18`

This will add 18 to the value of the variable "m" and then print out the result.

To assign the compute value to a variable you can use the equal sign without spaces around it:

m=`expr $m + 18`

Another way to evaluate arithmetic expressions is to use double parenthesis. For example:

(( m*=4 ))

This will quadruple the value of the variable "m".

Besides arithmetic evaluation, the Bash shell provides other programming constructs, such as for-loops, while-loops, conditionals, and functions and subroutines.

Was this page helpful?