Re: for loop with variable

"Mark J. Reed" <[email protected]> Mon, 11 May 2026 15:13:11 -0400
Newsgroups gmane.comp.shells.zsh.user
Message-ID <CAA=-s3wYMw6Ou7nq4soGGszvDyMf7ZW7FJZY97OoheDmBwu_Mw@mail.gmail.com>
--00000000000083177506518f8af5
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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=3D3; 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=3D({a..c})
for aa in $var; do # no =3D needed


On Mon, May 11, 2026 at 12:45=E2=80=AFPM Ray Andrews <[email protected]=
a> wrote:

> % var=3D" a b c "
>
> % for aa in $=3Dvar; 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?  Tha=
t
> is, the range '[a-c]' would be understood to be just text, not a file
> glob?
>


--=20
Mark J. Reed <[email protected]>

--00000000000083177506518f8af5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Standard brace expansion uses commas to separate individua=
l options and double-dots to separate ranges. Using commas instead of space=
s doesn&#39;t save any typing for single lists, but since you can put multi=
ple such expansions together, which turns into _every possible combination_=
, it can save a lot of typing indeed:<div><br></div><blockquote style=3D"ma=
rgin:0 0 0 40px;border:none;padding:0px"><div><font face=3D"monospace">% pr=
int -l {a,b,c}{1,2,3}</font></div></blockquote><blockquote style=3D"margin:=
0 0 0 40px;border:none;padding:0px"><div><font face=3D"monospace">a1</font>=
</div><div><font face=3D"monospace">a2</font></div><div><font face=3D"monos=
pace">a3</font></div><div><font face=3D"monospace">b1</font></div><div><fon=
t face=3D"monospace">b2</font></div><div><font face=3D"monospace">b3</font>=
</div><div><font face=3D"monospace">c1</font></div><div><font face=3D"monos=
pace">c2</font></div><div><font face=3D"monospace">c3</font></div></blockqu=
ote><div><div><br></div><div>Instant &quot;outer product&quot;, as the nerd=
s say.=C2=A0</div><div><br></div><div>It also works in bash and ksh.=C2=A0A=
lthough the timing is wonky in bash; brace expansion happens=C2=A0_before_ =
parameters, so this doesn&#39;t work:</div></div><div><br></div><blockquote=
 style=3D"margin:0 0 0 40px;border:none;padding:0px"><div><font face=3D"mon=
ospace">$ x=3D3; echo {1..$x}</font></div><div><font face=3D"monospace"><b>=
ksh, zsh</b>: 1 2 3</font></div><div><font face=3D"monospace"><b>bash</b>: =
{1..3}</font></div></blockquote><div style=3D"font-family:monospace"><font =
face=3D"monospace"><br></font></div><font face=3D"arial, sans-serif">Also, =
FWIW, if you&#39;re going to put your options into a variable, I recommend =
an array:</font><br><div><font face=3D"arial, sans-serif"><br></font></div>=
<blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div><font =
face=3D"monospace">var=3D({a..c})</font></div><div><font face=3D"monospace"=
>for aa in $var; do # no =3D needed</font></div></blockquote></div><br><div=
 class=3D"gmail_quote gmail_quote_container"><div dir=3D"ltr" class=3D"gmai=
l_attr">On Mon, May 11, 2026 at 12:45=E2=80=AFPM Ray Andrews &lt;<a href=3D=
"mailto:[email protected]">[email protected]</a>&gt; wrote:<br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>

 =20

   =20
 =20
  <div>
    <font face=3D"DejaVu Sans">% var=3D&quot; a b c &quot;<br>
      <br>
      % for aa in $=3Dvar; do echo $aa; done<br>
      a<br>
      b<br>
      c<br>
      <br>
      % for aa in [a-c]; do echo $aa; done=C2=A0<br>
      c<br>
      <br>
      ... In the last &#39;for&#39; it wants to find files whereas in the
      previous it knows it&#39;s looking at text.=C2=A0 Could the previous =
be
      done on one line?=C2=A0 That is, the range &#39;[a-c]&#39; would be u=
nderstood
      to be just text, not a file glob?=C2=A0=C2=A0</font>
  </div>

</blockquote></div><div><br clear=3D"all"></div><div><br></div><span class=
=3D"gmail_signature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_s=
ignature">Mark J. Reed &lt;<a href=3D"mailto:[email protected]" target=3D=
"_blank">[email protected]</a>&gt;<br></div>

--00000000000083177506518f8af5--