Get startedGet started for free

Arrays in Bash

1. Arrays in Bash

In this lesson we'll explore arrays. Let's get started!

2. What is an array?

There are two types of arrays in Bash. The first and most common is a numerical-indexed array. These are known as lists in Python or vectors in R. You may be familiar with creating these in Python, or R.

3. Creating an array in Bash

There are two ways to create a numerical-indexed array in Bash. You can construct the array without any data elements. You use the word 'declare', the 'a' flag and an array name. Or you could create and add elements at the same time like this, similar to string or numerical variable creation. Remember, don't add space around the equals sign.

4. Be careful of commas!

Unlike many programming languages, Bash separates array elements with spaces and not commas. This is not correct. This is correct as it uses spaces between array elements.

5. Important array properties

Bash has some useful array properties. You can return all array elements by indexing into the array using the at symbol. The length of an array is returned by adding a pound symbol to the front of a call to the entire array.

6. Manipulating array elements

Accessing an array element uses square bracket syntax. In our example, to return the third element we would write this. The third element is returned successfully. Bash uses zero-indexing for arrays. This is just like Python (but different from R).

7. Manipulating array elements

You can change array values using index notation. Here, we create an array. Then change the first element to be 999 and print to confirm. Don't use the $ index notation when overwriting a single element as this won't work.

8. Manipulating array elements

You can also slice out parts of an array using this notation. The notation adds to the call to print the entire array with a colon, number, colon, number. N and M mean the starting index and the number of elements to return after that index. Here, we start at the third index ( or fourth element) and return 2 elements. Which returns as so.

9. Appending to arrays

Appending to arrays uses the plus-equals syntax, ensuring the elements you want to add are surrounded by parentheses. In this example we create an array of numbers, and add ten to the end. Then we print out to check. Indeed, ten is added to the end.

10. (Not) appending to arrays

What happens if you don't wrap the elements you want to append in parentheses? Let's see. We repeat the same example, but don't use parentheses when adding 10. The string 10 is just added to our first element. This is not what we want at all.

11. Associative arrays

The second type of array in Bash is an associative array. It's index is made of strings that are keys with associated values. This is similar to a dictionary in Python or a list in R. Note this is only available in Bash 4 onwards. In Python you would create a dictionary like so. And a list like this in R.

12. Creating an associative array

Creating an associative array has slightly different syntax to a normal array. You must use the declare syntax. Either declare first and then add elements later or declare and add in one line. You set keys using square brackets and the values after the equals sign. You may add multiple elements at once.

13. Associative array example

Let's make an associative array of properties about a city. We declare first using the correct flag and call the array city_details. We set the keys to be the 'city_name' and the 'population' with these associated values. We can index into this array using any of the keys. Here we index using the city_name key and it returns what we expect. New York.

14. Creating an associative array

The second method is to declare and add elements all on the same line. Everything else such as rules rules around square parentheses, equals signs etc. are followed. Associative arrays have an extra property to normal arrays. You can return the keys using an exclamation sign like here. We see the keys returned.

15. Let's practice!

Now you have developed your understanding of arrays in Bash - let's practice using them in some practical settings!