While I don't write in Bash, I do know that you don't need more than two variables, at the most, to compute the Fibonacci sequence. In fact, you're very close to doing so.
Just sort of manipulating your code here,
#!/bin/bash # first was never used, it was not required second=1 # Somewhat confusing name, consider something different third=1 # Same here. echo -n "Term: " read -r term # Using the -r argument was recommended after research # Notice this functions without the beginning section for ((i=1; i<term; i++)) do echo $second second=$((second+third)) echo $third third=$((second+third)) done
If you were to apply this code to your picture above, simply replace all instances of fub
with second
.
Edit:
The modified code should display 2*term-2 numbers
, (Your code as it is now displays more, 2*term+1
numbers)
To change this so it only displays term
numbers, change term
so it's the proper upper limit after you've read it.
So, the code could look something like this:
for ((i=1; i<term/2+1; i++))
Tested it and yes, the code does work.