
$ factorial() { eval let N=1 N*={1..$1} ; echo $N; }
Nov. 29, 2019, 9:06 p.m.
—
CK
Explanation
Again, using let , this takes advantage of its mathematical functions and argumentbased variable assignments to serially overwrite the last known value of N with the product of N and the next integer generated by the expansion of {1..$1} into a list of incremental numbers. However, the order in which things are expanded and substituted means that {1..$1} would fail to expand into a list without using the eval command. It provides a layer of globbing that, once the shell has substituted in the value of the parameter $1 , will allow the braces to expand.
Thus the expression passed to eval is equivalent to:
let N=1 N=N*1 N=N*2 N=N*3 ... N=N*$1
