加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – 如何更改没有循环的bash数组元素的值

发布时间:2020-12-15 09:14:01 所属栏目:安全 来源:网络整理
导读:array=(a b c d) 我想在数组的每个元素之前添加一个字符为了有这个 array=(^a ^b ^c ^d) 一个简单的方法是循环使用数组元素并逐个更改值 for i in "${#array[@]}"do array[i]="^"array[i]done 但我想知道是否有任何方式做同样的事情没有循环数组,因为我必须
array=(a b c d)

我想在数组的每个元素之前添加一个字符为了有这个

array=(^a ^b ^c ^d)

一个简单的方法是循环使用数组元素并逐个更改值

for i in "${#array[@]}"
do
    array[i]="^"array[i]
done

但我想知道是否有任何方式做同样的事情没有循环数组,因为我必须做所有的元素相同的指令。

提前致谢。

使用参数扩展:
array=("${array[@]/#/^}")

从文档:

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. 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. Normally only the first match is replaced. If pattern begins with #,it must match at the beginning of the expanded value of parameter. If pattern begins with %,it must match at the end of the expanded value of parameter. If string is null,matches of pattern are deleted and the / following pattern may be omitted. 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读