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

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

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

You can test this from the command line by entering the following code:

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:

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 *script1.sh*, you can set the permissions to make the file executable with the command:

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:

or subtraction:

or division:

or modulo (the remainder after an integer division):

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:

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

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

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

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

Additionally, you can use the *increment* and *decrement* operators:

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

is equivalent to "m = m - 1".

And then there is the ternary "question mark-colon" operator, which returns one of two values depending on whether the specified condition is true or false. For example

The right-hand side of this assignment statement evaluates to "0" if the variable "m" is less than 9. Otherwise, it evaluates to 1. This means the variable "k" is assigned "0" if "m" is less than 9 and "1" otherwise.

The general form of the question mark-colon operator is:

### 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:

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:

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:

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

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.