Use wildcard expansion to echo all variables in zsh

旧时模样 提交于 2019-12-23 17:59:41

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/59007726/use-wildcard-expansion-to-echo-all-variables-in-zsh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!