Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array in shell?

Tags:

linux

shell

Now I use an ugly way to create arrays in shell, e.g.

ARG_ARRAY=(num1 num2 num3 num4 num5 num6 num7 num8 num9 num10)

Can this be more elegant ? like the C way, e.g.

ARG_ARRAY=num[10]
like image 443
Yifan Zhang Avatar asked Feb 28 '12 12:02

Yifan Zhang


People also ask

Are there arrays in shell?

There are two types of arrays that we can work with, in shell scripts. The default array that's created is an indexed array. If you specify the index names, it becomes an associative array and the elements can be accessed using the index names instead of numbers. Notice the uppercase and lowercase letter a.

How do you create an array?

You can make an array of int s, double s, or any other type, but all the values in an array must have the same type. To create an array, you have to declare a variable with an array type and then create the array itself. Array types look like other Java types, except they are followed by square brackets ( [] ).


1 Answers

$ ARG_ARRAY=(num{1..10})

$ echo ${ARG_ARRAY[@]}
num1 num2 num3 num4 num5 num6 num7 num8 num9 num10
like image 199
kev Avatar answered Nov 09 '22 04:11

kev