Stuber
Stuber

Reputation: 477

Use wildcard expansion to echo all variables in zsh

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

Upvotes: 4

Views: 717

Answers (1)

Stuber
Stuber

Reputation: 477

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

Upvotes: 2

Related Questions