Use wildcard expansion to echo all variables in zsh
Asked Answered
A

1

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
Alcibiades answered 23/11, 2019 at 12:40 Comment(4)
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.Yorkshire
I've posted a related question, though it's possible there is another solution for this.Yorkshire
It could be done with typeset -m 'zzz*' . Details here though: https://mcmap.net/q/1213513/-zsh-equivalent-of-bash-name-or-nameGratulation
@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.Supersedure
A
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
Alcibiades answered 24/11, 2019 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.