Math is easy, Bash scripting is easy, so performing math/arithmetic in Bash/Shell scripts should be easy too. It is. Here’s how to do it.
Working with Integers
Bash’s built-in arithmetic can only handle integer (whole number) values. If you attempt to declare a variable with a non-integer value:
declare -i e=2.5
You’ll see the following:
bash: declare: 2.5: syntax error: invalid arithmetic operator (error token is ".5")
To work with non-integer numbers, you will need to use an external program to perform your calculations – but first, here’s how to use built-in Bash arithmetic to work with integers.
Using Variable Declaration
If your variables were declared as integers using the declare statement, you could perform arithmetic without any special consideration:
# Declaring variables as integers declare -i x=4 declare -i y=2 # Performing arithmetic with integer variables result=x/y echo $result
This will return:
2
The mathematical statement doesn’t need to be wrapped in brackets for evaluation if variables are declared as integers.
Using Double Brackets/Parenthesis
If your variables weren’t specifically declared as integers, were passed as script parameters, or output from a program, they will be treated as strings by default.
Bash has the ability to deal with strings in arithmetic – but only if the expression is flagged up as being an arithmetic expression using double brackets. See the below example:
x = 6 ((y=$x+3)) echo $y
The above Bash script snippet will output the correct evaluation of the expression:
9
…as the bracketed arithmetic expression has been properly interpreted as containing variables with numbers, rather than treating them as strings.
Without the brackets:
y=$x+3
You would get the output:
6+3
The expression would be treated as joining two strings and not be evaluated.
Supported Mathematical Operators
Bash built-in arithmetic supports the following operators:
+ | Addition |
– | Subtraction |
++ | Increment |
— | Decrement |
* | Multiplication |
/ | Division |
% | Remainder |
** | Exponentiation |
Boolean and arithmetic
Double brackets are also seen when using boolean operators, as the evaluated expression will return 0 or 1:
if (( x > y )); then echo "x is greater than y" fi
See more about Bash Boolean Operations here
Working with Decimal Values
Using the bc Command
As shown earlier in the article, using Bash arithmetic with non-integer values will result in an error.
The bc command can handle decimal values (and more complex math). Here’s the syntax:
bc OPTIONS FILE
Where:
- OPTIONS are a list of options (see here for the full list of options)
- FILE is the path to a text file containing the arithmetic to be solved.
As bc will accept input being piped to it, you don’t have to store your equation in a file; you can pipe it in directly from the console:
echo "2.32+3.45" | bc
…which will output the correct answer of:
5.77
Here’s a full rundown on piping and redirecting command-line applications input and output.
You can view the full bc user manual by running:
man bc
There is a lot of advanced syntax for complex math for the bc command, which is outlined in detail in the user manual.