如何添加一个字符串到猛砸数组的每个元素? [英] How to append a string to each element of a Bash array?

查看:131
本文介绍了如何添加一个字符串到猛砸数组的每个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bash的一个数组,每个元素是一个字符串。我怎么可以追加另一个字符串的每个元素?在Java中,code是这样的:

 的for(int i = 0; I< array.length,我++)
{
    数组[I] .append(内容);
}


解决方案

测试,它的工作原理:

  =阵列(A B C D E)
CNT = $ {#数组[@]}
为((i = 0; I< CNT;我++));做
    数组[我] =$ {数组[我]} $ I
    回声$ {数组[我]}
DONE

生产:

  A0
B1
C2
D3
E4

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

  =阵列({} a..e)

为了帮助您了解数组和其在bash语法参考是一个良好的开端。此外,我建议你庆典,黑客解释。

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:

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

解决方案

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.

这篇关于如何添加一个字符串到猛砸数组的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆