This is my code to write a function which prints the Fibonacci numbers
function fib {
fib_array=( 0 1 )
count=3
while [[ $count -le $1 ]]
do
fib_new=$(( ${fib_array[-1]} + ${fib_array[-2]} ))
fib_array+=( $fib_new )
let count=$count+1
done
echo " ${fib_array[*]}"
}
When I run my bash with fib 5, I keep getting the messages :
-bash: fib_array: bad array subscript
-bash: fib_array: bad array subscript
-bash: + : syntax error: operand expected (error token is " ")
I assume the there might be some problems in the 1st line within the while do, i.e. fib_new=$(( ${fib_array[-1]} + ${fib_array[-2]} )) but don't know how to modify.
Can anyone help?
0 1 1 2 3.NEWSfile correctly