Arithmetic operators
Arithmetic operators are used for mathematical operations. The same logic what you have learned earlier on the programming courses. Note that Bash has limitation: it can only handle integer values! (use bc command with decimal values). The most common artihmetic operations are presented in the following table:
+
= addition-
= substraction/
= division*
= multiplication%
= modulus++
= increase the variable by 1--
= decrease variable by 1=
= assignments
There are multiple ways to perform arithmetic operations using Bash: using a double paranthesis ((( ))
) or 2 commands (let and expr).
There are no guidelines what you have must use, check what suits to your style the most and remember that there are multiple ways to do things with a little twists.
In most use cases arithmetic operation results are saved to the variable, but it's not necessary. Some of the following functions automatically prints the results after the expression.
Basic syntaxes:
$(( expression ))
let expression
expr item1 operator item2
echo "expression" | bc
#!/bin/bash
#Double parenthesis examples
A=$(( 4+3 ))
echo $A #7
#The basic format formated with spaces
A=$((4+3))
echo $A #7
# Basic format without spaces inside parenthesis
B=$(( A + 3 ))
echo $B #10
#Variable included without a dollar sign ($)
B=$(( $A + 3 ))
echo $B #10
#Variable included with a dollar sign ($)
(( A++ ))
echo $A #8
#Variable incremented by 1, same as A=A+1, no need for a dollar sign
(( A += 4 ))
echo $A #12
#Variable incremented by 4, same as A=A+4
D=$(( 10 * 2 ))
echo $D #20
# normal usage of the multiplication
#--------------------------------------------------------------------
#let function examples
let B=10-3
echo $B #7
# Basic format
let "B = 5 - 3"
echo $B #2
# quotes were used that allow to use spaces to make it more readable
let B--
echo $B #1
#Variable decreased by 1
let "C = $B + 100"
echo $C #101
#--------------------------------------------------------------------
# expr function examples
# Note that expr automatically print the result but can be used as command subtitution to save the result to a variable
expr 1 + 4 #5
#Basic format with spaces between and without quotes
expr "1 + 4" #1+4
# quotes around the expression, no evaluation just a print
expr 1+4 #1+4
# No spaces between items, no evaluation just a print
expr 20 \* 2 #40
#Note the character before the multiplication mark!
#There is a backslash to remove special character * meaning (escape) in Bash
C=$( expr 5 + 3 )
echo $C #8
# Saving the result in the variable, using expr as a subtitution
#--------------------------------------------------------------------
# bc examples ("easiest" way to do arithmetic operations with decimal values)
echo "10/3" | bc
# 3
# echo experession as a string and pipe it to a bc command
echo "5.5/3" | bc -l
# 1.83333333......
# -l option loads math lib (default scale is 20)
value=$(echo "scale=3; 10/3" | bc)
#3.333
# manually setup a special variable 'scale' value to desired decimal precision ans save the stdout to a variable
Variable length
Not a actual operation , but many times you need to find out how many charaters (length) is your variable value before you can do something.
Syntax: ${#variable}
#!/bin/bash
# Get the lenght of the variable value
A="Hi all students!"
echo ${#A} #16
B=30082021
echo ${#B} #8