且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Bash 中不指定索引的情况下向数组添加新元素

更新时间:2022-05-11 01:43:47

是的:

ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')

Bash 参考手册:

在赋值语句为 shell 变量或数组索引赋值的上下文中(请参阅数组),+="运算符可用于附加或添加到变量的先前值.

In the context where an assignment statement is assigning a value to a shell variable or array index (see Arrays), the ‘+=’ operator can be used to append to or add to the variable's previous value.