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

Re: this should be easy variable expansion including globs.

On Wed, 18 Jan 2017 08:13:54 -0800
Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> $ var1=var1
> $ var2=var2
> $ var3=var3
> $ var99=var99
> $ for f ($var*) echo $f

I'm going to regret this, but...

The right way of doing this is to use an array.  It's very similar to
what you've got except you refer to $var[1] rather than $var1.  That
index means the shell knows roughly what you've got on your mind from
the start.

The following syntax may look too good to be true, but does work...


This is equivalent to

typeset -a var

The first line is there to ensure var is an array.  This is a useful
example as it shows that the array grows as you need it to.

Now the simple "echo" you've got above can be done as

print -lr -- $var

The -l prints one entry per line.  The -r stops any clever expansions so
you get exactly what's in the array.

For most operations, you probably need more control over what you're
doing with entries.  Depending how complicated it gets, your main choices
are the following.

Process every non-empty array entry, regardless of number:

for elt in $var; do
  # $elt in turn refers to elements of the array
  print -r -- $elt

Process every entry whether it's empty or not --- there's no distinction
between the two with the values above, it's just a bit of arcanery in
case you need it.

for elt in "${var[@]}"; do
  print -r -- $elt

Loop over all 99 elements of... well, anything, but in this case that

integer i
for (( i = 1; i <= 99; i++ )); do
  print -r -- $var[i]

If you nonetheless still want to do some completely different, someone
else will no doubt be along in a minute for the usual loooooooong


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