4

zshに配列があるとします

a=(1 2 3)

.txt各要素に追加したい

echo ${a}.txt # this doesn't work

したがって、出力は

1.txt 2.txt 3.txt

アップデート:

私はこれを行うことができると思いますが、もっと慣用的な方法があると思います:

for i in $a; do
    echo $i.txt
done
4

1 に答える 1

6

オプションを設定する必要がありRC_EXPAND_PARAMます:

$ setopt RC_EXPAND_PARAM
$ echo ${a}.txt
1.txt 2.txt 3.txt

zsh マニュアルから:

RC_EXPAND_PARAM (-P)
              Array  expansions of the form `foo${xx}bar', where the parameter xx is set to
              (a b c), are substituted  with  `fooabar  foobbar  foocbar'  instead  of  the
              default  `fooa  b  cbar'.   Note that an empty array will therefore cause all
              arguments to be removed.

^フラグを使用して、1 つの配列拡張に対してのみこのオプションを設定することもできます。

$ echo ${^a}.txt
1.txt 2.txt 3.txt
$ echo ${^^a}.txt
1 2 3.txt

再びzshマニュアルを引用:

${^spec}
              Turn on the RC_EXPAND_PARAM option for the evaluation of spec; if the `^'  is
              doubled,  turn it off.  When this option is set, array expansions of the form
              foo${xx}bar, where the parameter xx is set to (a b c), are  substituted  with
              `fooabar foobbar foocbar' instead of the default `fooa b cbar'.  Note that an
              empty array will therefore cause all arguments to be removed.
于 2014-10-23T23:30:55.440 に答える