Indexed arrays and associative arrays
Bash provides a feature to declare a list (or array) of variables in a one-dimensional array that can be an indexed array or associative array. The size of an array can be 0
or more.
Indexed arrays
An indexed array contains variables that may or may not have been initialized continuously. Indices of an indexed array start from 0
. This means that the first element of an array will start at an index 0
.
Array declaration and value assignment
An indexed array can be declared by just initializing any index as follows:
array_name[index]=value
Here, an index can be any positive integer or an expression must be evaluated to a positive integer.
Another way of declaring is by using the declare
shell built in as follows:
declare -a array_name
We can also initialize an array with values during a declaration. Values are enclosed within parentheses and each value is separated with a blank space as follows:
declare -a array_name=(value1 value2 value3 …)