0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Square Root of a Number

Use the bc command with the -l option in Bash like this: echo "scale=4; sqrt(number)" | bc -l to find the square root of a number.
📋

Examples

Input16
Output4.0000
Input10
Output3.1622
Input0
Output0.0000
🧠

How to Think About It

To find the square root in Bash, use the calculator tool bc which supports math functions. We tell it to calculate the square root with sqrt() and set the decimal precision with scale. This avoids complex manual calculations.
📐

Algorithm

1
Get the input number from the user
2
Use the <code>bc</code> command with <code>-l</code> option to enable math library
3
Set the decimal precision using <code>scale</code>
4
Calculate the square root using <code>sqrt()</code> function
5
Print the result
💻

Code

bash
#!/bin/bash
read -p "Enter a number: " num
result=$(echo "scale=4; sqrt($num)" | bc -l)
echo "Square root of $num is $result"
Output
Enter a number: 16 Square root of 16 is 4.0000
🔍

Dry Run

Let's trace input 16 through the code

1

Read input

num=16

2

Calculate square root

echo "scale=4; sqrt(16)" | bc -l => 4.0000

3

Print result

Output: Square root of 16 is 4.0000

StepOperationValue
1Input read16
2Calculate sqrt4.0000
3Print outputSquare root of 16 is 4.0000
💡

Why This Works

Step 1: Use bc for math

The bc command is a calculator that supports math functions like sqrt() when run with -l option.

Step 2: Set decimal precision

The scale=4 sets the number of digits after the decimal point to 4 for a clear result.

Step 3: Calculate and print

We pass the expression to bc and capture the output to print the square root.

🔄

Alternative Approaches

Using awk
bash
#!/bin/bash
read -p "Enter a number: " num
result=$(awk "BEGIN {print sqrt($num)}")
echo "Square root of $num is $result"
This uses awk's built-in sqrt function but may have less control over decimal places.
Using Python from Bash
bash
#!/bin/bash
read -p "Enter a number: " num
result=$(python3 -c "import math; print(f'{math.sqrt(float($num)):.4f}')")
echo "Square root of $num is $result"
Calls Python for calculation, useful if bc or awk are not available but requires Python installed.

Complexity: O(1) time, O(1) space

Time Complexity

Calculating square root with bc or awk is a constant time operation since it uses built-in math functions.

Space Complexity

Only a few variables are used to store input and output, so space complexity is constant.

Which Approach is Fastest?

Using bc or awk is fast and native to shell environments; calling Python adds overhead but offers more flexibility.

ApproachTimeSpaceBest For
bc with -lO(1)O(1)Simple shell math with precision control
awk sqrt()O(1)O(1)Quick math without extra dependencies
Python callO(1) but slower startupO(1)Complex math or when Python is preferred
💡
Always use scale with bc to control decimal precision for square root results.
⚠️
Forgetting to use the -l option with bc which enables math functions like sqrt().