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

标签 bash arrays

有没有办法在 bash 中做类似 PHPs $array[] = 'foo'; 的事情:

array[0]='foo'
array[1]='bar'

最佳答案

是的,有:

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

Bash Reference Manual :

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.

还有:

When += is applied to an array variable using compound assignment (see Arrays below), the variable's value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array's maximum index (for indexed arrays)

关于bash - 在不指定 Bash 中的索引的情况下向数组添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1951506/

相关文章:

bash - 在 vim 中打开某些文件类型之前如何发出警告?

linux - 如何在 Linux 中操作重启一个 bash shell 脚本

bash - 如何通过 SSH 自动向多台服务器并行运行命令?

java - 搜索字符串数组

PHP 数组返回的计数超出了应有的数量?

linux - 两个相互依赖的后台进程

linux - 为什么循环会死掉?

javascript - 在 javascript 中使用特定键反转对象数组时出现问题(React)

javascript - 使用 json_encode() 和 JSON.parse 后地址数组输出错误

python - numpy 选择不同长度的每一行