简体   繁体   English

声明数组但不定义数组(Shell脚本)

[英]Declare array but not define the array ( Shell scripting)

There are alot of guides , guides out there which show how to declare and define an array 有很多指南指南显示如何声明和定义数组

foo[0] = "abc" 
foo[1] = "def"

What i am trying to achieve is to declare an array but not define it because it does not have to be define immediately , in most programming languages it will look something like this 我想要实现的是声明一个数组但不定义它,因为它不必立即定义,在大多数编程语言中它看起来像这样

int bar[100];

Is this possible in shell scripting lanuage ?? 这可能是shell脚本语言吗?

You can do that using bash declare statement 你可以使用bash declare语句来做到这一点

declare -a bar

And then you can add values using 然后你可以使用添加值

bar[0]=1
bar[1]=2
bar[2]=3

And print it using 并使用它打印

echo ${bar[0]}
echo ${bar[1]}
echo ${bar[2]}

to output

1
2
3

You can read more here 你可以在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM