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

Re: for loop with variable



Standard brace expansion uses commas to separate individual options and double-dots to separate ranges. Using commas instead of spaces doesn't save any typing for single lists, but since you can put multiple such expansions together, which turns into _every possible combination_, it can save a lot of typing indeed:

% print -l {a,b,c}{1,2,3}
a1
a2
a3
b1
b2
b3
c1
c2
c3

Instant "outer product", as the nerds say. 

It also works in bash and ksh. Although the timing is wonky in bash; brace expansion happens _before_ parameters, so this doesn't work:

$ x=3; echo {1..$x}
ksh, zsh: 1 2 3
bash: {1..3}

Also, FWIW, if you're going to put your options into a variable, I recommend an array:

var=({a..c})
for aa in $var; do # no = needed

On Mon, May 11, 2026 at 12:45 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
% var=" a b c "

% for aa in $=var; do echo $aa; done
a
b
c

% for aa in [a-c]; do echo $aa; done 
c

... In the last 'for' it wants to find files whereas in the previous it knows it's looking at text.  Could the previous be done on one line?  That is, the range '[a-c]' would be understood to be just text, not a file glob?  


--
Mark J. Reed <markjreed@xxxxxxxxx>


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