4

With multiple variables beginning with the same pattern can a wildcard be used to echo all matched patterns?

when zzz1=test1; zzz_A=test2; zzza=test3

What is the best way to match all variables starting with zzz. Where something like echo $zzz* or for i in $zzz*; do echo $i; done would output:

test1
test2
test3
Stuber
  • 447
  • 5
  • 16
  • I know in `bash` you can use `${!zzz*}` to expand to all variable names that start with `zzz`. There should be something similar in `zsh`, though I don't recall what it is. – chepner Nov 23 '19 at 13:28
  • I've posted a [related question](https://stackoverflow.com/q/59008514/1126841), though it's possible there is another solution for this. – chepner Nov 23 '19 at 14:17
  • 2
    It could be done with `typeset -m 'zzz*'` . Details here though: https://stackoverflow.com/a/59009819/4387039 – hchbaw Nov 23 '19 at 16:48
  • @Stuber: If you just want to see variable names starting with a certain stringdisplayed on the command line, you can use _variable completion_: If you type, say, `echo $zzz` and then a TAB character, Zsh shows all variables starting with `zzz`. Ensure that `setopt auto_list` is set. – user1934428 Nov 26 '19 at 07:48

1 Answers1

2

So to directly answer based on comments above... No, zsh cannot expand and echo variables using a wildcard, but typeset can provide the desired result.

typeset -m 'zzz*' outputs:

zzz_A=test2
zzz1=test1
zzza=test3

or more accurately to get my desired output as explained here:

for i in `typeset +m 'zzz*'`; do echo "${i}:  ${(P)i}"; done
zzz1:  test1
zzz_A:  test2
zzza:  test3

or just...

for i in `typeset +m 'zzz*'`; do echo "${(P)i}"; done
test1
test2
test3
Stuber
  • 447
  • 5
  • 16