How to expand the elements of an array in zsh? -
say have array in zsh
a=(1 2 3)
i want append .txt
each element
echo ${a}.txt # doesn't work
so output is
1.txt 2.txt 3.txt
update:
i guess can this, think there's more idiomatic way:
for in $a; echo $i.txt done
you need set rc_expand_param
option:
$ setopt rc_expand_param $ echo ${a}.txt 1.txt 2.txt 3.txt
from zsh manual:
rc_expand_param (-p) array expansions of form `foo${xx}bar', parameter xx set (a b c), substituted `fooabar foobbar foocbar' instead of default `fooa b cbar'. note empty array hence cause arguments removed.
you can set alternative for 1 array expansion using ^
flag:
$ echo ${^a}.txt 1.txt 2.txt 3.txt $ echo ${^^a}.txt 1 2 3.txt
again citing zsh manual:
${^spec} turn on rc_expand_param alternative evaluation of spec; if `^' doubled, turn off. when alternative set, array expansions of form foo${xx}bar, parameter xx set (a b c), substituted `fooabar foobbar foocbar' instead of default `fooa b cbar'. note empty array hence cause arguments removed.
arrays zsh variable-expansion
No comments:
Post a Comment