0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Calculate Compound Interest Easily

Use a Bash script with the formula amount = principal * (1 + rate/100/n)^(n*time) using bc -l for floating-point math, for example: amount=$(echo "$principal * (1 + $rate / 100 / $n) ^ ($n * $time)" | bc -l).
📋

Examples

Inputprincipal=1000, rate=5, time=2, n=1
Output1102.50
Inputprincipal=1500, rate=4.3, time=6, n=4
Output1938.84
Inputprincipal=2000, rate=0, time=5, n=12
Output2000.00
🧠

How to Think About It

To calculate compound interest, think of starting with the principal amount and repeatedly increasing it by the interest rate divided by the number of times interest is compounded per year. Multiply this growth factor by itself for the total number of compounding periods (n times time). Use a calculator or tool that supports decimal math to get the final amount.
📐

Algorithm

1
Get input values for principal, annual interest rate, time in years, and number of times interest compounds per year.
2
Calculate the compound interest factor: (1 + rate / 100 / n).
3
Raise this factor to the power of (n * time).
4
Multiply the principal by this result to get the final amount.
5
Print the final amount rounded to two decimal places.
💻

Code

bash
#!/bin/bash
read -p "Enter principal amount: " principal
read -p "Enter annual interest rate (in %): " rate
read -p "Enter time in years: " time
read -p "Enter number of times interest compounded per year: " n
amount=$(echo "$principal * (1 + $rate / 100 / $n) ^ ($n * $time)" | bc -l)
printf "Compound Interest Amount: %.2f\n" "$amount"
Output
Enter principal amount: 1000 Enter annual interest rate (in %): 5 Enter time in years: 2 Enter number of times interest compounded per year: 1 Compound Interest Amount: 1102.50
🔍

Dry Run

Let's trace the example with principal=1000, rate=5, time=2, n=1 through the code

1

Input values

principal=1000, rate=5, time=2, n=1

2

Calculate compound factor

1 + 5 / 100 / 1 = 1.05

3

Calculate power

1.05 ^ (1 * 2) = 1.1025

4

Calculate amount

1000 * 1.1025 = 1102.5

StepCalculationResult
11 + 5/100/11.05
21.05 ^ 21.1025
31000 * 1.10251102.5
💡

Why This Works

Step 1: Use of formula

The formula amount = principal * (1 + rate/100/n)^(n*time) calculates how money grows with compound interest.

Step 2: Floating-point math

Bash cannot do decimal math natively, so bc -l is used to handle floating-point calculations.

Step 3: Rounding output

The printf command formats the output to two decimal places for currency style.

🔄

Alternative Approaches

Using awk for calculation
bash
read -p "Principal: " p; read -p "Rate: " r; read -p "Time: " t; read -p "Compounds per year: " n; awk -v p=$p -v r=$r -v t=$t -v n=$n 'BEGIN {amount=p*(1+r/100/n)^(n*t); printf "Compound Interest Amount: %.2f\n", amount}'
Awk can do floating-point math without external tools, making the script simpler but less portable on minimal systems.
Using bc with scale for rounding
bash
read -p "Principal: " p; read -p "Rate: " r; read -p "Time: " t; read -p "Compounds per year: " n; amount=$(echo "scale=4; $p * (1 + $r / 100 / $n) ^ ($n * $t)" | bc -l); printf "Compound Interest Amount: %.2f\n" "$amount"
Setting scale in bc controls decimal precision before rounding with printf.

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

Time Complexity

The calculation involves a fixed number of arithmetic operations and a power function call, so it runs in constant time.

Space Complexity

Only a few variables are stored, so space usage is constant.

Which Approach is Fastest?

Using awk or bc both run quickly for this simple calculation; differences are negligible for single calculations.

ApproachTimeSpaceBest For
bc -lO(1)O(1)Precision and portability
awkO(1)O(1)Simpler syntax, no external calls
bc with scaleO(1)O(1)Control decimal precision explicitly
💡
Always use bc -l or awk for decimal math in Bash scripts.
⚠️
Trying to do floating-point math directly in Bash without using bc or awk causes incorrect results.