Reputation: 683
In C++, if you have two variables a and b, you can do this to add b to a:
a += b;
How can you do the same thing in bash?
Upvotes: 0
Views: 2231
Reputation: 11581
If you use the (( ... ))
syntax, you don't need to use $
at all before most (simple) variables, so you can do:
a=$((a + b))
or
((a += b))
Upvotes: 1
Reputation: 1591
Surround the expression in double parenthesis, like so:
a=$(($a + $b))
Alternatively, you could do something like:
(( a+= $b ))
or even:
let a+=$b
Upvotes: 2
Reputation: 8725
#!/bin/bash
echo "enter two numbers:"
read a b
let a+=$b
echo $a
Upvotes: 1
Reputation: 1797
Well, a +=b
is just a = a + b
.
In bash, you can use the following syntax:
a=$(($a+ $b))
Upvotes: 0