- Syntax error: use no quotes inside arithmetic evaluation.
- Logic error: you are mixing STDOUT and
returnvalues.
Either pass values as STDOUT:
function factorial { (( $1 )) && echo $(( $1 * $( factorial $(( $1 - 1 )) ) )) || echo 1 } factorial 5 Or return them:
function factorial { (( $1 )) || return 1 factorial $(( $1 - 1 )) return $(( $1 * $? )) } factorial 5 echo $? Both codes work in bash, ksh (93 sure, no idea about 88) and zsh, so I guess yes, shells do support recursion.