I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below:
#!/bin/bash
Fibo() {
case $1 in
0) echo 0;;
1) echo 1;;
*) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;;
esac
}
for (( i=0; i<=$1; i++ )); do
Fibo $i
done
Here's a sample output:
0
1
1
3
5
7
9
11
Of course, it should be 0 1 1 2 3 5 8 13 ...
I tried to write it down in the paper and in my opinion it should work. My code understanding:
Fibo 0 -> = 0
Fibo 1 -> = 1
Fibo 2 -> Fibo 1 + Fibo 0 = 1 + 0 = 1
Fibo 3 -> Fibo 2 + Fibo 1 = 1 + 1 = 2
Fibo 4 -> Fibo 3 + Fibo 2 = 2 + 1 = 3
Fibo 5 -> Fibo 4 + Fibo 3 = 3 + 2 = 5
etc...
What the code "is doing":
Fibo 0 -> = 0
Fibo 1 -> = 1
Fibo 2 -> = 1 + 0 = 1
Fibo 3 -> = 3 (??)
Fibo 4 -> = 5 (this is probably 3+1+1)
Fibo 5 -> = 7 (this is probably once again 5+1+1)
Fibo 6 -> = 9 (once again 7+1+1)
Can you please help me find out where the error lies? I know there's been many Fibonacci-alike threads already (and I tried to follow the advice given) but I did not find any answer to my problem.
Regards, B