在不指定Bash索引的情况下向数组添加新元素 [英] Add a new element to an array without specifying the index in Bash
本文介绍了在不指定Bash索引的情况下向数组添加新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在bash中做类似PHP $array[] = 'foo';
的事情:
Is there a way to do something like PHPs $array[] = 'foo';
in bash vs doing:
array[0]='foo'
array[1]='bar'
推荐答案
是的:
ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')
在赋值语句为外壳变量或数组索引赋值的情况下(请参见Arrays),可以使用"+ ="运算符来追加或添加到变量的前一个值.
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.
这篇关于在不指定Bash索引的情况下向数组添加新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文