Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

bash - 在不指定Bash索引的情况下向数组添加新元素(Add a new element to an array without specifying the index in Bash)

Is there a way to do something like PHPs $array[] = 'foo';

(有没有办法像PHP一样$array[] = 'foo';)

in bash vs doing:

(在bash vs做:)

array[0] = 'foo'
array[1] = 'bar'
  ask by Darryl Hein translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes there is:

(就在这里:)

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

Bash Reference Manual :

(Bash参考手册 :)

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.

(在赋值语句为外壳变量或数组索引赋值的上下文中(请参见Arrays),可以使用'+ ='运算符来追加或添加到变量的先前值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...