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

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.

相关文章

普通模式 >G 增加当前行到文档末尾处的缩紧层级 $ 移动到本行的末尾 . 相当于一个...
原文连接: https://spacevim.org/cn/layers/lang/elixir/ 模块简介 功能特性 启用模块 快捷键 语言专属...
原文连接: https://spacevim.org/cn/layers/lang/dart/ 模块简介 功能特性 依赖安装及启用模块 启用模...
 =   赋值操作符,可以用于算术和字符串赋值 +        加法计算     -        减法运算...
1.根据包名来查看指定的APP指定数据 adb shell "top | grep com.xxx.xxx" 由于这样打印出来的数...
ctrl+F 向下翻页 ctrl+B 向下翻页 u 取消最近一次操作 U 取消当前行的操作 ZZ 保存当前内容并退出 gg 跳...