Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 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 Reply

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),可以使用'+ ='运算符来追加或添加到变量的先前值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...