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

Re: counting in "for" loops



Sven Guckes <guckes@xxxxxxxxxxxxxxxxx> writes:

> Again, I look at the manual and  I cannot find
> how to do a simple count within a for loop.
> 
> 	$ for count in ???; do
> 	> echo -n $count
> 	> done
> 	1 2 3 4 5 6 7 8 9 10

This works for me (with zsh-3.1.4):

for ((count=1; count<=10; count++)); do
echo $count
done

> Why isn't this in the manual as an example?  *sigh*

The manuals don't have many examples, currently.  By all means
contribute things that you'd like to see in future versions, and this
example seems useful.  Although something like:

for ((count=0; count<10; count++))

would be a more common sort of requirement.

Alternatively {1..10} will expand to a list containing the numbers, so

for count in {1..10}; do
...

might be sufficient for what you want.  Doing the same for
{1..1000000} and things will take more memory than is optimal, though.



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