简体   繁体   English

bash中数组的内联扩展

[英]Inline expansion of arrays in bash

I'm trying to expand an array in bash: 我正在尝试在bash中扩展一个数组:

FILES=(2009q{1..4})
echo ${FILES[@]}
echo ${FILES[@]}.zip

Output is: 输出是:

2009q1 2009q2 2009q3 2009q4
2009q1 2009q2 2009q3 2009q4.zip

But how can I expand the last line as in echo 2009q{1..4}.zip expansion, so that the last line looked like: 但是如何在echo 2009q{1..4}.zip扩展中扩展最后一行,以便最后一行看起来像:

2009q1.zip 2009q2.zip 2009q3.zip 2009q4.zip

... but using array FILES ? ...但是使用数组FILES

FILES=(2009q{1..4})
echo ${FILES[@]/%/.zip}

Output: 输出:

2009q1.zip 2009q2.zip 2009q3.zip 2009q4.zip

From Bash's Parameter Expansion : 从Bash的参数扩展

${parameter/pattern/string} : The pattern is expanded to produce a pattern just as in filename expansion. ${parameter/pattern/string} :扩展模式以生成模式,就像在文件名扩展中一样。 Parameter is expanded and the longest match of pattern against its value is replaced with string. 扩展参数,并将模式与其值的最长匹配替换为字符串。 If pattern begins with '/', all matches of pattern are replaced with string. 如果pattern以'/'开头,则pattern的所有匹配都将替换为string。 Normally only the first match is replaced. 通常只替换第一场比赛。 If pattern begins with '#', it must match at the beginning of the expanded value of parameter. 如果pattern以'#'开头,则它必须在参数的扩展值的开头匹配。 If pattern begins with '%', it must match at the end of the expanded value of parameter. 如果pattern以'%'开头,则它必须在参数的扩展值的末尾匹配。 If string is null, matches of pattern are deleted and the / following pattern may be omitted. 如果string为null,则删除pattern的匹配,并且可以省略/ following模式。 If parameter is '@' or ' ', the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. 如果参数是'@'或' ',则替换操作依次应用于每个位置参数,并且扩展是结果列表。 If parameter is an array variable subscripted with '@' or ' ', the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list. 如果parameter是一个用'@'或' ' 下标的数组变量 ,则替换操作依次应用于数组的每个成员,并且扩展是结果列表。

You can use printf : 你可以使用printf

printf "%s.zip " "${FILES[@]}"

2009q1.zip 2009q2.zip 2009q3.zip 2009q4.zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM