Bash math uses $(( )) for integers and bc or awk for decimals. Everything else is string manipulation until you opt into arithmetic context.
Integer math
a=10
b=3
echo $((a + b))
echo $((a / b))
echo $((a % b))Division is integer-only inside $(( )).
Increment
n=0
((n++))
echo "n=$n"
if (( n > 0 )); then echo "positive"; fi(( )) is arithmetic test/command—returns exit status like other commands.
Floating point
echo "scale=2; 10/3" | bcFor money or science, consider Python (Python track)—Bash is not a calculator for all numeric types.
Important interview questions and answers
- Q: Why $(( ))?
A: Arithmetic expansion—no external expr command needed for integers. - Q: Integer division of 10/3?
A: 3 in Bash arithmetic, not 3.333.
Self-check
- What syntax adds two variables?
- Which tool handles decimal division in the example?
Tip: Use Python or bc for floats—Bash integers only in $(( )).
Interview prep
- $(( ))?
Integer arithmetic expansion.
- bc for?
Decimal math when Bash integers are insufficient.