$ bang() ( IFS=\*; let N=$1-1 k="$*" && bang $N $k || echo ${2-1} ) ## ExplanationA recursive function that leverages the arithmetic expansions evaluated by the -
concatenates the arguments`IFS=\*` `"$*"` using a`*` symbol, which`let` interprets as a multiplication operator; -
assigns arithmetic values to variables`let N=$1-1 k="$*"` `N` and`k` .`N` will have a value one less than the first argument;`k` will be the product of all the arguments; -
performs the command immediately to its right iff the expression immediately to its left evaluates to`&&` `true` ; -
calls itself with the first argument being one less than it was, and the second argument being the cumulative product of the arguments thus far across all iterations;`bang $N $k` -
performs the command immediately to its right iff the command immediately to its left wasn't performed;`||` -
prints the second argument, which—if there is one—will be the factorial, otherwise it prints`echo ${2-1}` `1` .
## LimitationsIf a positive integer is not the one and only argument passed to it when invoked by the user, the result will be nonsense or an error will be thrown. |