如何将字符串附加到Bash数组的每个元素?

时间:2022-12-11 13:44:30

I have an array in Bash, each element is a string. How can I append another string to each element? In Java, the code is something like:

在Bash中有一个数组,每个元素都是一个字符串。如何向每个元素添加另一个字符串?在Java中,代码是这样的:

for (int i=0; i<array.length; i++)
{
    array[i].append("content");
}

4 个解决方案

#1


13  

Tested, and it works:

测试和工作原理:

array=(a b c d e)
cnt=${#array[@]}
for ((i=0;i<cnt;i++)); do
    array[i]="${array[i]}$i"
    echo "${array[i]}"
done

produces:

生产:

a0
b1
c2
d3
e4

EDIT: declaration of the array could be shortened to

编辑:数组的声明可以缩短为

array=({a..e})

To help you understand arrays and their syntax in bash the reference is a good start. Also I recommend you bash-hackers explanation.

为了帮助您理解bash中的数组及其语法,引用是一个良好的开端。同时,我也向您推荐bash-hackers的解释。

#2


73  

You can append a string to every array item even without looping in Bash!

即使在Bash中没有循环,也可以将字符串附加到每个数组项!

# cf. http://codesnippets.joyent.com/posts/show/1826
array=(a b c d e)
array=( "${array[@]/%/_content}" )
printf '%s\n' "${array[@]}"

#3


56  

As mentioned by hal

如前所述,哈尔

  array=( "${array[@]/%/_content}" )

will append the 'content' string to each element.

将“content”字符串附加到每个元素。

  array=( "${array[@]/#/prefix_}" )

will prepend 'prefix_' string to each element

每个元素的前缀' prefix_string

#4


2  

You pass in the length of the array as the index for the assignment. The length is 1-based and the array is 0-based indexed, so by passing the length in you are telling bash to assign your value to the slot after the last one in the array. To get the length of an array, your use this ${array[@]} syntax.

将数组的长度作为赋值的索引进行传递。长度是基于1的,数组是基于0的索引,所以通过在数组中传递长度,您就告诉bash将您的值分配给数组中最后一个槽的位置。要获得数组的长度,可以使用这个${array[@]}语法。

declare -a array
array[${#array[@]}]=1
array[${#array[@]}]=2
array[${#array[@]}]=3
array[${#array[@]}]=4
array[${#array[@]}]=5
echo ${array[@]}

Produces

生产

1 2 3 4 5

#1


13  

Tested, and it works:

测试和工作原理:

array=(a b c d e)
cnt=${#array[@]}
for ((i=0;i<cnt;i++)); do
    array[i]="${array[i]}$i"
    echo "${array[i]}"
done

produces:

生产:

a0
b1
c2
d3
e4

EDIT: declaration of the array could be shortened to

编辑:数组的声明可以缩短为

array=({a..e})

To help you understand arrays and their syntax in bash the reference is a good start. Also I recommend you bash-hackers explanation.

为了帮助您理解bash中的数组及其语法,引用是一个良好的开端。同时,我也向您推荐bash-hackers的解释。

#2


73  

You can append a string to every array item even without looping in Bash!

即使在Bash中没有循环,也可以将字符串附加到每个数组项!

# cf. http://codesnippets.joyent.com/posts/show/1826
array=(a b c d e)
array=( "${array[@]/%/_content}" )
printf '%s\n' "${array[@]}"

#3


56  

As mentioned by hal

如前所述,哈尔

  array=( "${array[@]/%/_content}" )

will append the 'content' string to each element.

将“content”字符串附加到每个元素。

  array=( "${array[@]/#/prefix_}" )

will prepend 'prefix_' string to each element

每个元素的前缀' prefix_string

#4


2  

You pass in the length of the array as the index for the assignment. The length is 1-based and the array is 0-based indexed, so by passing the length in you are telling bash to assign your value to the slot after the last one in the array. To get the length of an array, your use this ${array[@]} syntax.

将数组的长度作为赋值的索引进行传递。长度是基于1的,数组是基于0的索引,所以通过在数组中传递长度,您就告诉bash将您的值分配给数组中最后一个槽的位置。要获得数组的长度,可以使用这个${array[@]}语法。

declare -a array
array[${#array[@]}]=1
array[${#array[@]}]=2
array[${#array[@]}]=3
array[${#array[@]}]=4
array[${#array[@]}]=5
echo ${array[@]}

Produces

生产

1 2 3 4 5