← Back to Bash Course | Chapter 4: Loops | Lesson 13 of 14

Math Functions

In this page:

  1. Math Functions

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

bash
#!/bin/bash
echo "2^10 = $((2 ** 10))"

sqrt_result=$(echo "sqrt(16)" | bc -l)
echo "sqrt(16) = $sqrt_result"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.