在 Bash 中不指定索引的情况下向数组添加新元素 [英] Add a new element to an array without specifying the index in Bash

查看:27
本文介绍了在 Bash 中不指定索引的情况下向数组添加新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 bash 中做类似 PHPs $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')

Bash 参考手册:

在赋值语句为 shell 变量或数组索引赋值的上下文中(请参阅数组),+="运算符可用于附加或添加到变量的先前值.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆