This is my attempt:
#!/bin/bash
function fibonacci(){
first=$1
second=$2
if (( first <= second ))
then
return 1
else
return $(fibonacci $((first-1))
As Wumpus said you need to produce output using for example echo
.
However you also need to fix the recursive invocation.
The outermost operation would be an addition, that is you want:
echo $(( a + b ))
Both a
and b
are substitutions of fibonacci
, so
echo $(( $(fibonacci x) + $(fibonacci y) ))
x
and y
are in turn arithmetic expressions, so each needs its own $(( ))
, giving:
echo $(( $(fibonacci $((first-1)) ) + $(fibonacci $((second-2)) ) ))
If you are confused by this, you should put the components into temporary variables and break down the expression into parts.
As to the actual fibonacci, it's not clear why you are passing 2 arguments.