how can I add (subtract, etc.) two numbers with bash?
Arithmetic in POSIX shells is done with $
and double parentheses (( ))
:
echo "$(($num1+$num2))"
You can assign from that (sans echo
):
num1="$(($num1+$num2))"
There is also expr
:
expr $num1 + $num2
In scripting $(())
is preferable since it avoids a fork/execute for the expr
command.
The existing answer is pure bash, so it will be faster than this, but it can only handle integers. If you need to handle floats, you have to use the external program bc
.
$ echo 'scale=4;3.1415+9.99' | bc
13.1315
The scale=4
tells bc
to use four decimal places. See man bc
for more information.
You can also use $[ ... ]
structure. In this case, we use built-in mechanizm in Bash, which is faster and a bit more convenient to use. Since we know that everything between $[, and ] is treated as an expression, we don't need to precede the variables with $
. Similarily, we do not need to secure *
from treating it like a pattern.
num1=2
num2=3
echo $[num1 + num2]
5