0

I have a program on bash which calculates n'th number of Fibonacci seq. But keep getting an error expr: syntax error. As I understand this is because I return something from function in wrong way, but I dont understand how to make it right. Here is my code:

fib() {
    if (( $1 < 2 ))
    then
        return 1
    fi

    c=$(fib `expr $1 - 1`)
    b=$(fib `expr $1 - 2`)

    echo "$b $c" 
    #Here I have a blank line in output
    #So I thought may be the problem is about returning/reception values?

    return `expr $c + $b`
}
PepeHands
  • 1,368
  • 5
  • 20
  • 36
  • 1
    From [Returning Values from Bash Functions](http://www.linuxjournal.com/content/return-values-bash-functions): _"Bash functions, unlike functions in most programming languages do not allow you to return a value to the caller"_. Just assign a value and then check it. Otherwise, `echo` something and catch it doing `myvar=$(fib)`. – fedorqui Sep 29 '14 at 13:32
  • 2
    Also relevant: [How to Return a String Value from a Bash Function](http://stackoverflow.com/questions/3236871/how-to-return-a-string-value-from-a-bash-function?rq=1) – kojiro Sep 29 '14 at 13:34
  • 1
    @fedorqui thanks a lot for this article! I understood whats going on) – PepeHands Sep 29 '14 at 13:36

0 Answers0