Our Knowledge Base provides step-by-step guides, troubleshooting tips, and expert insights to help you manage VPS, dedicated servers, domains, DDoS protection, and more — all designed to make your experience with us fast, secure, and stress-free.
Bash supports one-dimensional numerically indexed and associative arrays types. Numerical arrays are referenced using integers, and associative are referenced using strings.
Numerically indexed arrays can be accessed from the end using negative indices, the index of -1 references the last element. The indices do not have to be contiguous.
Unlike most of the programming languages, Bash array elements don’t have to be of the same data type. You can create an array that contains both strings and numbers.
Bash does not support multidimensional arrays, and you can’t have array elements that are also arrays.
There is no limit on the maximum number of elements that can be stored in an array.
Arrays in Bash can be initialized in different ways.
Bash variables are untyped, any variable can be used as an indexed array without declaring it.
To explicitly declare an array, use the declare builtin:
One way to create an indexed array is by using the following form:
Where index_* is a positive integer.
Another way to create a numeric array is to specify the list of the elements within parentheses, separated by empty space:
When the array is created using the form above, indexing starts at zero i.e. the first element have an index of 0.
Unlike numerically indexed, the associative arrays must be declared before they can be used.
To declare an associative array use the declare builtin with the -A (uppercase) option:
Associative arrays can be created using the following form:
Where index_* can be any string.
You can also create an associative array using the form below:
Bash arrays syntax may look a little strange at first, but it will make more sense once you read this article.
To reference a single element, you need to know the element index.
Any element can be referenced using the following syntax:
The syntax for accessing an array element is similar to the syntax of most of the programming languages. The curly braces ${} are required to avoid shell’s filename expansion operators.
Let’s print the element with index of 1:
If you use @ or * as an index, the word expands to all members of the array. To print all elements you would use:
The only difference between @ and * is when the form ${my_array[x]} is surrounded with double-quotes. In this case, * expands to a single word where array elements are separated with space. @ expands each array element to a separate word. This is especially important when using the form to illiterate through array elements.
To print the keys of the array add the ! operator before the array name:
Here is an example:
To get the length of an array, use the following form:
The syntax is the same as when referencing all elements wit addition of the # character before the array name.
The most common way to iterate over each item in an array is by using the for loop:
The code above will iterate over the array and print each element in a new line:
Here is an example of how to print all keys and values:
Another way to loop through an array is to get the length of the array and use the C style loop:
To add a new element to a bash array and specify its index use the following form:
Here is an example:
Another way of adding a new element to an array without specifying the index is by using the += operator. You can add one or multiple elements:
declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
To delete a single element, you’ll need to know the element index. An element can be removed using the unset command:
Let’s see an example:
We’ve explain how to create numerically indexed and associative arrays. We have also show how to iterate through the arrays, calculate the array length, and add and remove elements.