Arrays in bash scripts can be indexed or associative. There is a simple syntax that can be used to create indexed arrays with ease, and the declare bash built in command can be used to create associative arrays. These are the two kinds of arrays that are supported in bash, and there is no support of multidimensional arrays in bash.
There is not just the question of how to go about creating an array in bash, but there is also how to go about looping over them, and preform all kinds of various tasks that one might be familial with in other languages. Keep in mind that there is much that is lacking when it comes to things like class methods for example. However the basic functionality that one might expect in a language such as bash is very much there.
There are several ways to create indexed arrays where each element of the array has a numbered rather than named key value. In this section I will just be quickly going over a few ways to go about creating these kinds of arrays in bash. In the process of doing so I will also be touching base on basic curly bracket parameter expansion which is the way that one will want to go about accessing element values of an array once one is created.
One pretty simple way to go about creating a basic indexed array is to just use a bracket syntax with a variable name. That is just create a simple variable as always, but just place an opening and closing set of square brackets at the end of the variable name and before equals. Then place the desired index value for the element that will be assigned for that index in between the square brackets.
Simple enough, but there are a few other ways to go about creating an array in the first place. Also this is just one example of how to create an indexed array, if I want to create an associative array it can not be done this way.
Expressions are another way to create indexed Arrays. This is just placing the desired values for each element in between a set of parentheses with a space used as a way to separate them.
To create an associative array the declare bash built in command can be used with the -A option to do so. Once that is done the process of adding elements is the same as indexed ones, only I just give a named key value rather than a numbered one.
So now that I got the basics out of the way when it comes to creating arrays, maybe not I should cover how to go about looping over the contents of them with for in loops. There are just a few simple examples to cover when it comes to the basics of using this kind of loop with arrays that mainly just have to do with looping values, and looping key names of Arrays.
When it comes to looping over the values of an indexed array there is just using the curly bracket syntax with the at symbol when it comes to the value after typing in.
To loop over the values of an an associative array I just need to do more or less the same thing as I would with an indexed Array.
3.3 - Get key value pairs of an Associative Array with braces parameter expansion and a little variable indirection
So the same bash code can be used to get values with any kind of array, but what if I want to get both key values as well as the names. To get both key values as well as the key names a little variable indirection as it is called in the bash man page can be used.
Of course the best source on Arrays in bash, and using bash in general might be to check out the man page on bash. Of course the man page is one real long sucker and it can take a while to read through the whole thing, but if you really want to learn bash that will be the man source.
There are a number of other posts on Arrays in bash on the open Internet of course also where the topic is just on arrays alone though. Also it is true that a man page does not always do the best job of conveying something, and it can never hurt to have more actual examples of a certain something with bash.