有没有办法在 bash 中做类似 PHPs $array[] = 'foo';
的事情:
array[0]='foo'
array[1]='bar'
最佳答案
是的,有:
ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')
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/