Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Can you loop in math mode?



2019-08-19 02:16:20 +0200, Sebastian Gniazdowski:
[...]
> is some kind of a looping possible in math mode?
[...]

Can you maybe be more specific as to what you're trying to do?

You can use recursion:

$ n=6 a='(n-->1 ? a*(n+1) : 1)' zsh -c 'echo $((a))'
720

(6!)

Including via math functions, but within limits.

$ n=0 a='(++n <= 255 ? a+2 : 1)' zsh -c 'echo $((a))'
zsh:1: math recursion limit exceeded: (++n <= 255 ? a+2 : 1)

And you can of course have command substitutions inside an
arithmetic expression:

$ echo $(( 1 $(for i ({2..6}) print "*$i") ))
720

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author