如何启动数组元素为0,在bash? [英] How to initiate array element to 0 in bash?

查看:150
本文介绍了如何启动数组元素为0,在bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

declare -a MY_ARRAY=()

请问数组的声明以这种方式在bash将启动所有的数组元素为0?

Does the declaration of array in this way in bash will initiate all the array elements to 0?

如果没有,如何启动数组元素为0?

If not, How to initiate array element to 0?

推荐答案

您例子声明/初始化一个空数组。

Your example will declare/initialize an empty array.

如果您要初始化数组成员,你做这样的事情:

If you want to initialize array members, you do something like this:

declare -a MY_ARRAY=(0 0 0 0) # this initializes an array with four members

如果你想用100个成员初始化数组,你可以这样做:

If you want to initialize an array with 100 members, you can do this:

declare -a MY_ARRAY=( $(for i in {1..100}; do echo 0; done) )

请记住,在bash中数组不固定长度的(也不指数必须是连续的)。因此,你不能初始化的数组中的所有的成员,除非你知道电话号码应该是什么。

Keep in mind that arrays in bash are not fixed length (nor do indices have to be consecutive). Therefore you can't initialize all members of the array unless you know what the number should be.

这篇关于如何启动数组元素为0,在bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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