gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:51 25 4
gpt4 key购买 nike

有没有办法在 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/37877299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com