Re: for loop with variable

"Mark J. Reed" <[email protected]>
Newsgroups gmane.comp.shells.zsh.devel
Message-ID <CAA=-s3wYMw6Ou7nq4soGGszvDyMf7ZW7FJZY97OoheDmBwu_Mw__15672.1047636651$1778526912$gmane$org@mail.gmail.com>
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 <[email protected]> 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 <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.