1
2if [[ ! "$#" -eq 1 || ! "$1" =~ ^[0-9]+$ ]]; then
3 echo "Usage: $0 <n>"
4 echo "where <n> is a positive integer."
5 exit 1
6fi
7
8
9fibonacci() {
10 local n="$1"
11 local a=0
12 local b=1
13 local i
14
15 for ((i=1; i<=n; i++)); do
16 c="$((a + b))"
17 a="$b"
18 b="$c"
19 done
20
21 echo "$a"
22}
23
24
25nth_fibonacci="$(fibonacci "$1")"
26echo "The $1th Fibonacci number is: $nth_fibonacci"
27