0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Factorial of a Number

You can find the factorial of a number in Bash using a loop like this: factorial=1; for (( i=1; i<=num; i++ )); do factorial=$((factorial * i)); done.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial, multiply all whole numbers from 1 up to the given number. Start with 1 and keep multiplying by each number until you reach the input number.
📐

Algorithm

1
Get the input number from the user
2
Initialize a variable to 1 to hold the factorial result
3
Loop from 1 to the input number
4
Multiply the factorial variable by the current loop number each time
5
After the loop ends, output the factorial result
💻

Code

bash
#!/bin/bash
read -p "Enter a number: " num
factorial=1
for (( i=1; i<=num; i++ ))
do
  factorial=$((factorial * i))
done
echo "Factorial of $num is $factorial"
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Initialize factorial

factorial = 1

2

Loop i=1

factorial = 1 * 1 = 1

3

Loop i=2

factorial = 1 * 2 = 2

4

Loop i=3

factorial = 2 * 3 = 6

5

Loop i=4

factorial = 6 * 4 = 24

6

Loop i=5

factorial = 24 * 5 = 120

ifactorial
11
22
36
424
5120
💡

Why This Works

Step 1: Initialize factorial

Start with factorial=1 because multiplying by 1 does not change the value.

Step 2: Multiply in loop

Multiply factorial by each number from 1 to the input using factorial=$((factorial * i)).

Step 3: Output result

After the loop, print the final factorial which is the product of all numbers up to the input.

🔄

Alternative Approaches

Recursive function
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"
Uses recursion which is elegant but can be slower and limited by stack size.
Using bc for large numbers
bash
#!/bin/bash
read -p "Enter a number: " num
factorial=1
for (( i=1; i<=num; i++ ))
do
  factorial=$(echo "$factorial * $i" | bc)
done
echo "Factorial of $num is $factorial"
Uses bc to handle very large numbers beyond Bash integer limits.

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

Time Complexity

The loop runs from 1 to n, so the time grows linearly with the input number.

Space Complexity

Only a few variables are used, so space stays constant regardless of input size.

Which Approach is Fastest?

The iterative loop is fastest and simplest; recursion adds overhead and bc handles big numbers but slower.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Simple and fast for small to medium numbers
Recursive functionO(n)O(n)Elegant but limited by recursion depth
Using bcO(n)O(1)Handling very large numbers beyond Bash integer limits
💡
Always initialize factorial to 1 before multiplying in the loop.
⚠️
Forgetting to initialize factorial to 1 causes incorrect results.