> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Calculate Factorial of a Number
- URL: https://linuxhandbook.com/factorial-script/
- Published: 2024-06-07T09:20:22.000Z
- Updated: 2024-08-22T07:11:45.000Z
- Description: Here are a few sample example bash scripts to display the Fibonacci sequence based on a given input.
- Author: Abhishek Prakash
- Tags: Bash Practice Exercises, #docs-col

Time to practice your Bash scripting.

## Exercise

In this Bash practice exercise, write a shell script that accepts a non-negative number and shows its factorial as output. 

💡**Hint**: Factorial is calculated in this fashion:

```
N! = N x (N-1) x (N-2) x (N-3) x ... x 3 x 2 1 
```

Remember that the factorial of 0 is 1\. And you cannot have a factorial of negative numbers.

### Test data

If you input 5, you should get the following (5 x 4 x 3 x 2 x 1):

```
120
```

If you input 7, you should get the following:

```
5040
```

✋

A programming challenge can be solved in more than one way. The solution presented here is for reference purposes only. You may have written a different program and it could be correct as well.

## Solution 1: Factorial bash script using recursive function

Here's a sample bash scripting for getting factorial of a given number using only a for loop.

I have added the option step for checking that non-negative numbers are not enetered.

```
#!/bin/bash

read -p "Enter a non-negative number: " num

if [ $num -lt 0 ]; then
	echo "You entered a negative number"
	exit
fi

fact=1
for (( i=1; i<=$num; i++ )); do
	fact=$(($fact*$i))
done

echo "$num! is: $fact"
```

## Solution 2: Factorial script using factorial in bash

Here's a bash shell script that uses the concept of recursion and function:

```
#!/bin/bash

read -p "Enter a non-negative number: " num

if [ $num -lt 0 ]; then
	echo "You entered a negative number"
	exit
fi

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

echo -n "$num! is: "
factorial $num
```

💡

Notice the use of echo commands with and without `-n`? It is used to [stop echo from automatically adding a newline](https://linuxhandbook.com/echo-without-newline/).

## 📖 Concepts to revise

The solutions discussed here use some terms, commands and concepts and if you are not familiar with them, you should learn more about them.

- [read command](https://linuxhandbook.com/read-command/)
- [arithmetic operation in bash](https://linuxhandbook.com/bash-arithmetic-operators/)
- [if else in bash](https://linuxhandbook.com/if-else-bash/)
- for loop in bash
- [functions in bash](https://linuxhandbook.com/bash-functions/)

## 📚 Further reading

If you are new to bash scripting, we have a streamlined tutorial series on Bash that you can use to learn it from scratch or use it to brush up the basics of bash shell scripting.

[Bash Scripting Tutorial Series for Beginners \[Free\]Get started with Bash Shell script learning with practical examples. Also test your learning with practice exercises.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2023/09/bash.png)](https://linuxhandbook.com/bash/)