Bash Script to Find Power of a Number
Use a Bash script with a loop to multiply the base number by itself exponent times, like:
result=1; for ((i=0; i. Examples
Inputbase=2, exponent=3
Output8
Inputbase=5, exponent=0
Output1
Inputbase=3, exponent=4
Output81
How to Think About It
To find the power of a number, multiply the base by itself repeatedly as many times as the exponent says. Start with 1 because multiplying by 1 doesn't change the value. Then multiply the result by the base in each step until you reach the exponent count.
Algorithm
1
Get the base number and exponent from the user.2
Initialize a result variable to 1.3
Repeat multiplying the result by the base, exponent times.4
After the loop, output the result.Code
bash
#!/bin/bash read -p "Enter base number: " base read -p "Enter exponent: " exponent result=1 for ((i=0; i<exponent; i++)) do result=$((result * base)) done echo "$base raised to the power $exponent is $result"
Output
Enter base number: 2
Enter exponent: 3
2 raised to the power 3 is 8
Dry Run
Let's trace base=2 and exponent=3 through the code
1
Initialize result
result = 1
2
First loop iteration
result = 1 * 2 = 2
3
Second loop iteration
result = 2 * 2 = 4
4
Third loop iteration
result = 4 * 2 = 8
| Iteration | Result |
|---|---|
| 0 (initial) | 1 |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
Why This Works
Step 1: Start with 1
We set result=1 because multiplying by 1 keeps the value unchanged, serving as a neutral starting point.
Step 2: Multiply in loop
Each loop multiplies result by base, repeating this exponent times to build the power.
Step 3: Output final result
After the loop finishes, result holds the base raised to the exponent, which we print.
Alternative Approaches
Using bc command
bash
#!/bin/bash read -p "Enter base number: " base read -p "Enter exponent: " exponent result=$(echo "$base^$exponent" | bc) echo "$base raised to the power $exponent is $result"
This uses the external bc calculator for power, which handles large numbers and decimals but requires bc installed.
Using arithmetic expansion with ** operator (bash 4+)
bash
#!/bin/bash read -p "Enter base number: " base read -p "Enter exponent: " exponent result=$((base ** exponent)) echo "$base raised to the power $exponent is $result"
This uses Bash's built-in exponent operator, which is simpler but requires Bash version 4 or higher.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs exponent times, so the time grows linearly with the exponent.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
Using Bash's ** operator is fastest and simplest, while the loop method is more manual but compatible with older Bash versions.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop multiplication | O(n) | O(1) | Compatibility with all Bash versions |
| bc command | Depends on bc | O(1) | Handling large or decimal powers |
| Bash ** operator | O(1) | O(1) | Simple integer powers with modern Bash |
Use Bash's built-in
** operator for power if your Bash version supports it for simpler code.Forgetting to initialize the result to 1 causes incorrect multiplication results.