Recursive Fibonacci in Bash script

前端 未结 5 1370
名媛妹妹
名媛妹妹 2020-12-20 06:44

This is my attempt:

#!/bin/bash

function fibonacci(){

first=$1
second=$2

if (( first <= second ))
then
return 1

else 

return $(fibonacci $((first-1))         


        
5条回答
  •  我在风中等你
    2020-12-20 07:14

    The $(...) substitution operator is replaced with the output of the command. Your function doesn't produce any output, so $(...) is the empty string.

    Return values of a function go into $? just like exit codes of an external command.

    So you need to either produce some output (make the function echo its result instead of returning it) or use $? after each call to get the value. I'd pick the echo.

提交回复
热议问题