IMLC.ME
Search…
en-US
How to do math in Bash
Arithmetic Operator
Description
id++, id--
variable post-increment, post-decrement
++id, --id
variable pre-increment, pre-decrement
-, +
unary minus, plus
!, ~
logical and bitwise negation
**
exponentiation
*, /, %
multiplication, division, remainder (modulo)
+, -
addition, subtraction
<<, >>
left and right bitwise shifts
<=, >=, <, >
comparison
==, !=
equality, inequality
&
bitwise AND
^
bitwise XOR
|
bitwise OR
&&
logical AND
||
logical OR
expression ? expression : expression
conditional operator
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
assignment
1
let sum=1+2+3+4
2
echo "Sum: $sum"
3
4
i=0
5
i=$((++i))
6
echo "i: $i"
7
8
let minus=sum-i
9
echo "minus: $minus"
10
11
let division=minus/3
12
echo "division: $division"
13
14
let exponentiation=$((2**division))
15
echo "exponentiation: $exponentiation"
16
17
true=1
18
false=0
19
echo "\!$true=$((! true)) \!$false=$((! false))"
20
echo "$true&&$true=$((true&&true))"
21
echo "$true&&$false=$((true&&false))"
22
23
echo "Result: $((true ? 1000 : 2000))"
24
Copied!
Last modified 1yr ago
Copy link