Bash has two types of arrays: indexed arrays and associative arrays. For indexed arrays, the indexes begin from 0 to (n-1), as is common in most languages. However, arrays in Bash are sparse. This means that you can assign the (n-1)th array element without having assigned the (n-2)th element.

In this tutorial, you will learn how to work with arrays in Bash. Let’s get started.

Defining Arrays

There are three ways you can define arrays in Bash. Similar to Bash variables, arrays need to be initialized at creation. The only exception to this is if you’re using the declare keyword. You also need to be sure that no space is left on either side of the assignment operator as you’re initializing the array.

The first method is compound assignment of values to the array name. There are two ways to do this:

In the first compound assignment, the values in the round brackets are assigned sequentially from the index [0] to [3].

However, in the second, the values are assigned to an index in whichever order the programmer has indicated.

If you took close notice to arr2, you’ll notice that index [2] was left out. The array will still be created without any errors thrown. This assignment is actually a demonstration of sparse storage in Bash arrays as we touched on earlier.

Notice that there are no commas separating the array values. The values are simply separated by spaces.

The second method indirectly declares the array. You can just start assigning values to null array elements:

The third way is to explicitly declare the array with the keyword declare:

Operations on Arrays

To access array elements, use this syntax: ${array[index]}

If you need to print out the entire array instead, use the @ symbol as the

index of ${array[index]}:

To find out the number of elements in the array, use the # symbol as shown below:

You may also need to modify array elements—see the example below on how to do so. It’s similar to how you add a new element. The only difference is that you’re replacing a value to an index that already has a value.

Associative Arrays

An array that has arbitrary values as its keys is called an associative array. These arrays are used to store related key-value pairs.

To define an associative array, you need to do so explicitly using the keyword declare.

You can access a member element in the same way you do indexed arrays:

If you wish to print out all the values, you can use the @ symbol as shown below:

If you want to print out all the array keys, you can use the @ and ! symbols as demonstrated below:

To find the number of elements the associative array has, use the same syntax you’d use with indexed arrays (demonstrated in the last section).

If you wish to delete an array item or the entire array, use the syntax below:

Using the printf Command

You may have noticed that this whole article uses the echo command to output data to the shell. The echo command works for this tutorial but has few features and flexibility when it comes to string formatting.

However, the printf command offers more specific formatting options that make Bash scripting a breeze. Learning the printf function will surely enhance your string formatting experience and efficiency in Bash.