Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

How to work with arrays in Bash scripts

Tux, the Linux mascot, sitting with a laptop in front of a large terminal window.-1
Lucas Gouveia/How-To Geek

If you're working with a large amount of data in your Bash scripts, arrays will make your life a lot easier. Some people get intimidated by the syntax. But once learned, it will make your scripts more powerful, with fewer variables to think about.

Declaring an array

Arrays in Bash let you store multiple pieces of data in a single variable, much like a Python list . This makes them powerful tools for managing sets of related values such as filenames, usernames, configuration options, or command-line arguments.

Unlike many other programming languages, Bash doesn't require you to declare an array type explicitly. You simply assign values using specific syntax, and Bash knows it's an array. The declaration method varies depending on the array type (covered later.)

Advertisement
Advertisement

To declare an indexed array, which is the most common type of array, you simply type the name, an equal sign, and parentheses.

That's it. You just declared an empty array. You can see its elements using the echo command , like this:

Declaring an empty array and printing its content using the echo command.

Since there are no elements, you won't see anything except a blank line. You can assign values to the array in the same line. To do that, type space-separated elements inside the parentheses.

Each space-separated value becomes an element of the array. You can verify it with:

Declaring an array with elements and printing its content using the echo command.

You can also declare an empty array and add elements later.

Printing the array will display the newly added elements.

Declaring an empty array in Bash and adding elements later.

This approach is handy if you need to populate the array dynamically. You can also assign individual elements by index.

Another way you can declare an array is by using thedeclarebuilt-in to explicitly define an indexed array.

This makes it clear to others that 'colors' is intended as an array, which can be useful in larger scripts. To confirm whether a Bash variable is an array (and what kind), use thedeclare -pcommand.

Declaring an array using the declare builtin and checking its content.
Advertisement
Advertisement

This is particularly useful when debugging scripts.

Looping through an array

Once you've declared an array, you'll usually want to do something with its elements: print them, process them, or pass them to commands. In Bash, there are several ways to loop through arrays, and understanding these approaches is key to writing flexible, bug-free scripts.

The most common way to iterate through every element in an array is with a simple Bash for loop .

Iterating through a Bash array using a simple for loop.

This is great for going through the values. But what if you need the indices too? For that, you can use a C-style for loop, like this:

Iterating through a Bash array using a C style for loop.

This is ideal if you need both position and value. To get just the indices, you can use the special expansion${!array[@]}, like this:

Iterating through a Bash array indices using a for loop.

This technique is also handy when the array isn't sequential (for example, if some elements were removed). If your array comes from a command's output, awhile readloop can be more efficient or cleaner.

This approach is excellent when reading data dynamically from commands or files. Another cool trick is that you can traverse multiple arrays together. Here's an example:

Iterating through multiple Bash arrays using a for loop.
Advertisement
Advertisement

When looping multiple arrays together, always ensure they have the same length.

Different types of arrays in Bash

Bash arrays come in a few varieties, and understanding their differences helps you choose the right tool for your script. Bash supports two main array types:

  1. Indexed arrays: standard lists indexed by numbers.

  2. Associative arrays: key-value pairs indexed by strings.

There's also a special type worth noting called sparse arrays, where the numeric indices aren't sequential. Let's go through each type.

Indexed arrays

Indexed arrays are the most common type, perfect when you want to store ordered lists such as filenames, user IDs, or numbers. Each element is identified by an integer index starting at 0.

An example of an indexed array.

You can view all elements at once or check the number of elements:

Indexed arrays are useful when you have a list with a natural order, or you want to iterate through results from commands.

Associative arrays

Associative arrays act like dictionaries or hash maps in many other languages. They let you assign values to string keys instead of numeric indices.

An example of declaring an associative array and accessing its element.

You can loop through them easily:

Iterating through an associative array and printing each key value pair.

Note that the order of keys in associative arrays is undefined. If order matters, you'll need to sort them manually. Associative arrays are useful when you need to map relationships or handle JSON format data .

Sparse arrays

Sparse arrays are a lesser-known but interesting feature. They're indexed arrays with gaps in their numbering. You can assign elements to arbitrary indices. Bash doesn't require them to be sequential.

An instance of showcasing sparse arrays and their non-sequential characteristic.
Advertisement
Advertisement

Accessing unset indices will return nothing:

You can still loop through existing elements safely. If you have a large data set where not every index is used, or are working with results that naturally have "holes", then the sparse feature can often be useful.

Accessing, modifying, and deleting array elements

Once you've declared and populated an array, the next step is to learn how to get values out, update them, and remove them when needed. Working with array elements in Bash is straightforward, but there are a few subtle behaviors worth remembering.

You can access an element by its index (for indexed arrays) or key (for associative arrays.) For indexed arrays:

For associative arrays:

In both cases, if the index or key doesn't have a value, you get an empty string as an output. If you want to access all elements or indices at once, there are some special syntaxes. Here's a cheatsheet you can follow.

Advertisement
Advertisement

Always quote your expansions:"${array[@]}". Unquoted expansions can break when elements contain spaces or wildcards.

To modify an array element, simply reassign it:

Modifying Bash array elements by reassigning values.

You can also append new elements:

Appending an element to a Bash array.

For associative arrays, you reassign values to keys.

Modifying Bash associative array elements by reassigning values.

Reassigning a value overwrites the old one, so be careful when performing it.

To delete an element, you can use theunsetcommand. It can remove specific elements or the entire array.

Deleting an element from a Bash array using the unset command.
Advertisement
Advertisement

To remove multiple elements:

Deleting multiple elements from a Bash array using the unset command.

To delete the whole array, simply pass the array name:

The same is for associative arrays. Use the unset command and the key (instead of the index) to remove that element.

If you want to modify or remove multiple values, using a for loop to iterate through the array is a good idea.

Some advanced array tricks

By now, you know how to declare, loop through, and manipulate arrays. However, Bash can do a lot more with them. Let's look at a few advanced array techniques that can make your scripts smarter and easier to maintain.

Slicing

You can extract subsets of arrays using slice syntax.

Here, start refers to the first element in the slice, while length refers to how many elements you want to keep in the subset. Note that Bash arrays have 0-based indexing. Example:

An example of slicing a Bash array.

We have the middle three elements since we sliced starting from index one. This is useful when you want to process a portion of the results from a command.

Copying and combining

Copying an array simply requires you to assign the array elements to another new array.

Copying an array to a new array in Bash.

Bash copies arrays by value, not by reference. So, modifying 'copy' won't affect the original.

Advertisement
Advertisement

To merge two or more arrays into one, you pass their elements to a new array using the expansion syntax.

Combining two array elements into one array in Bash.

Sorting

Sorting an array means rearranging it in a specific order. This could be numerically or alphabetically (depending on the element type,) ascending or descending. Bash doesn't have a built-in sort function, but it's easy to achieve with command substitution and the sort command.

Sorting a Bash array alphabetically in ascending order.

Here, the sort command sorts the elements alphabetically.IFS=$''ensures elements containing spaces aren't split incorrectly. We store the sorted output into a new array.

Similarly, we can do reverse sorting as well.

In the case of numerical data, you can usesort -n.

Sorting a Bash array numerically in ascending order.

Removing duplicates

A classic problem. Remove any duplicate items in the array so that all remaining elements are unique. We can take help from the sort command again.

Removing duplicate elements from a Bash array.
Advertisement
Advertisement

This approach works by printing all elements, sorting them, and removing duplicates with -u. However, this method is order-dependent. It sorts your array in the process. If you need to keep the original order, you'll have to loop and track seen elements manually.

Filtering arrays

The last trick I'd like to cover is filtering elements from an array. We'll do this using pattern matching and parameter expansion.

Filtering certain elements from an array in Bash.

This trick is simple but powerful when handling mixed file lists or datasets.


With that, you should have a good grasp on how to use arrays in Bash to manipulate your data. This will help you write better scripts. You can get started by going through some example Bash scripts .

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: