3 ms·
zsh makes this so much easier. This doesn't capture $i, but covers most use cases of "I want to run something more than once": repeat 10; echo "repeat me" I
by arp242 3y ago
zsh makes this so much easier. This doesn't capture $i, but covers most use cases of "I want to run something more than once":
repeat 10; echo "repeat me"
If you do need $i you can use a similar construct as bash, but more convenient:
for ((i = 0; i < 10; i++)); echo $i
for i in {0..10}; echo $i # {n..m} loops also work in bash
for ((i = 0; i < 10; i++)); { echo $i; echo $i } # Multiple commands
Short loops are hugely helpful in interactive use because it's much less muckery for short one-liners (whether you should use them in scripts is probably a bit more controversial).
Also looping over arrays "just works" as you would expect without requiring special incantations:
arr=(a b)
for v in $arr; echo $v
---
Whether zsh or fish is better is a matter of taste, and arguably zsh has too many features, but IMHO bash is absolutely stuck as a "1989 ksh clone" (in more ways than one; it uses K&R C all over the place, still has asm hacks to make it run on 1980s versions of Xenix, and things like that).
- JNRowe 3y agoAny reason to eschew short_loops in general that you're aware of? I ask because I'd probably use `for i ({0..9}) echo $i` in your for loop example. I've never managed to get my head around the necessity for a narrow short_repeat option when there is - for example - no short_select. All of the zsh alternate forms feel far superior to me, in both interactive use and within scripts. JNRowe notes the many off-by-one translations in other examples and tips hat to arp242
- arp242 3y agoOh yeah, I forgot about the {n..m} syntax; I almost always use repeat these days or the C-style loop if I need $i, as that's more "in the fingers", so to speak, from other languages, even though {n..m} is easier. I don't know why you would want to avoid short loops, other than subjective stylistic reasons (which is completely valid of course). The patch that added short_repeat just asserts that "SHORT_LOOPS is bad"[1], but I don't now why the author thinks that. Also: my previous comment was wrong; you don't need "setopt short_repeat"; you only need it if you explicitly turned off short loops with "setopt no_short_loops". [1]: https://www.zsh.org/mla/workers/2019/msg01174.html https://www.zsh.org/mla/workers/2019/msg01174.html