Arithmetic in Bash
Integer math lives inside double parentheses.
a=17
b=5
# $(( )) is integer arithmetic; no sigils needed inside
echo "$((a + b)) $((a - b)) $((a * b))"
echo "quotient $((a / b)), remainder $((a % b))"
echo "$((2 ** 10))"
# increments work inside (( )) as a statement
(( a += 1 ))
echo "a is now $a"
echo "bigger: $(( a > b ? a : b ))"
How it works
$(( ))evaluates; variables need no sigils inside.- Division truncates;
%is the remainder;**is power. (( a += 1 ))as a statement mutates; the ternary picks a value.
Keywords and builtins used here
echo
The run, in numbers
- Lines
- 12
- Characters to type
- 289
- Tokens
- 74
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 56 seconds.
Step 3 of 3 in Variables & quoting, step 3 of 26 in Language basics.