Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive function in bash

I want to do a function that will return the factorial of a number in bash

Here's the current code that doesn't work, can anyone tell me what's wrong and how to correct it? I just started learning bash and I don't know that much.

#!/bash/bin factorial() {   let n=$1   if (( "$n" <= "1" ))   then return 1   else   factorial n-1   return $n*$?   fi   return 0 } factorial 5 echo "factorial 5 = $?" 
like image 649
FinalDestiny Avatar asked Mar 13 '12 10:03

FinalDestiny


People also ask

Can bash functions be recursive?

BASH supports function declaration and recursion – just like other modern programming language. See below the Recursive function to compute the factorial values. We use “local” to keep the variables at scope of function, and we can use $() to call a function recursively.

What is recursion in shell script?

Function Recursion - Shell ScriptingThis feature enables the function to be called recursively. Calling a function recursively is when the function calls itself to reach an answer. Usually, a recursive function has a base value that it eventually iterates down to.

What is a recursive function example?

Simple examples of a recursive function include the factorial, where an integer is multiplied by itself while being incrementally lowered. Many other self-referencing functions in a loop could be called recursive functions, for example, where n = n + 1 given an operating range.

What does #$ mean in bash?

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments). Follow this answer to receive notifications. edited Jul 9 at 13:55.


2 Answers

There are several syntax and a quite obvious logic one (return 0)

A working version is below:

#!/bin/bash  factorial() {     if (( $1 <= 1 )); then         echo 1     else         last=$(factorial $(( $1 - 1 )))         echo $(( $1 * last ))     fi } factorial 5 

You are missing:

  1. return is bad (should use echo)

  2. shbang line (is /bin/bash not /bash/bin)

  3. Can't do arithmetic outside of (( )) or $(( )) (or let, but (( )) is preferred)

like image 111
Daniel Voina Avatar answered Sep 28 '22 10:09

Daniel Voina


#!/bin/bash  function factorial()  {     if (( $1 < 2 ))    then      echo 1    else      echo $(( $1 * $(factorial $(( $1 - 1 ))) ))    fi } 

This will work better.

(It works up to 25, anyway, which should be enough to prove the point about recursion.)

For higher numbers, bc would be the tool to use, making the ninth line above:

echo "$1 * $(factorial $(( $1 - 1 )))" | bc 

but you have to be a bit careful with bc --

$ factorial 260 38301958608361692351174979856044918752795567523090969601913008174806\ 51475135399533485285838275429773913773383359294010103333339344249624\ 06009974551133984962615380298039823284896547262282019684886083204957\ 95233137023276627601257325925519566220247124751398891221069403193240\ 41688318583612166708334763727216738353107304842707002261430265483385\ 20637683911007815690066342722080690052836580858013635214371395680329\ 58941156051513954932674117091883540235576934400000000000000000000000\ 00000000000000000000000000000000000000000 

was quite a strain on my poor system!

like image 34
user1070300 Avatar answered Sep 28 '22 12:09

user1070300