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 = $?"
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.
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.
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.
#$ 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.
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:
return is bad (should use echo
)
shbang line (is /bin/bash not /bash/bin)
Can't do arithmetic outside of (( ))
or $(( ))
(or let
, but (( ))
is preferred)
#!/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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With