0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Factorial Using Recursion

You can find factorial using recursion in Bash by defining a function like factorial() { if [ $1 -le 1 ]; then echo 1; else local temp=$(( $1 - 1 )); local result=$(factorial $temp); echo $(( $1 * result )) ; fi } and calling it with a number.
📋

Examples

Input0
Output1
Input5
Output120
Input1
Output1
🧠

How to Think About It

To find factorial recursively, think of the problem as multiplying the number by the factorial of the number just before it. When the number reaches 1 or less, stop and return 1. This way, the function calls itself with smaller numbers until it reaches the base case.
📐

Algorithm

1
Get the input number.
2
Check if the number is less than or equal to 1; if yes, return 1.
3
Otherwise, call the factorial function with the number minus one.
4
Multiply the current number by the result of the recursive call.
5
Return the multiplication result.
💻

Code

bash
#!/bin/bash

factorial() {
  if [ $1 -le 1 ]; then
    echo 1
  else
    local temp=$(( $1 - 1 ))
    local result=$(factorial $temp)
    echo $(( $1 * result ))
  fi
}

read -p "Enter a number: " num
fact=$(factorial $num)
echo "Factorial of $num is $fact"
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace factorial(3) through the code

1

Call factorial(3)

3 > 1, so call factorial(2)

2

Call factorial(2)

2 > 1, so call factorial(1)

3

Call factorial(1)

1 <= 1, return 1

4

Return from factorial(2)

2 * 1 = 2

5

Return from factorial(3)

3 * 2 = 6

CallReturned Value
factorial(1)1
factorial(2)2
factorial(3)6
💡

Why This Works

Step 1: Base Case

The function stops calling itself when the input is 1 or less using if [ $1 -le 1 ], returning 1 to avoid infinite recursion.

Step 2: Recursive Call

For inputs greater than 1, the function calls itself with $1 - 1 to break down the problem into smaller parts.

Step 3: Multiplying Results

Each recursive call returns a factorial value that is multiplied by the current number to build the final factorial.

🔄

Alternative Approaches

Iterative approach
bash
#!/bin/bash
read -p "Enter a number: " num
fact=1
for (( i=2; i<=num; i++ ))
do
  fact=$(( fact * i ))
done
echo "Factorial of $num is $fact"
Uses a loop instead of recursion; easier to understand and avoids stack overflow for large numbers.
Using bc for large numbers
bash
#!/bin/bash
factorial() {
  if [ $1 -le 1 ]; then
    echo 1
  else
    echo "$1 * $(factorial $(( $1 - 1 )))" | bc
  fi
}
read -p "Enter a number: " num
fact=$(factorial $num)
echo "Factorial of $num is $fact"
Uses bc calculator to handle bigger numbers beyond Bash integer limits.

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

Time Complexity

The function calls itself once per number down to 1, so it runs in linear time proportional to the input number.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used grows linearly with the input.

Which Approach is Fastest?

The iterative approach is faster and uses less memory because it avoids recursive call overhead.

ApproachTimeSpaceBest For
RecursiveO(n)O(n)Learning recursion, small inputs
IterativeO(n)O(1)Performance and large inputs
bc CalculatorO(n)O(n)Handling very large numbers
💡
Always include a base case in recursion to prevent infinite loops.
⚠️
Forgetting the base case causes the function to call itself endlessly and crash.