Math Functions
In this page:
Math Functions
Bash itself has no built-in functions like square root or power beyond ** for integer exponentiation in $(( )). For anything more advanced, scripts shell out to bc -l (which supports sqrt(), s() for sine, etc.) or awk. Knowing when to delegate to an external tool is part of writing effective Bash.
Note: $(( 2 ** 10 )) computes integer powers directly without needing bc.
Example: Math Functions
#!/bin/bash
echo "2^10 = $((2 ** 10))"
sqrt_result=$(echo "sqrt(16)" | bc -l)
echo "sqrt(16) = $sqrt_result"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: