Arrays in Bash

Introduction to Bash Scripting

Alex Scriven

Data Scientist

What is an array?

Two types of arrays in Bash:

  • An array
    • 'Normal' numerical-indexed structure.
    • Called a 'list' in Python or 'vector' in R.

In Python: my_list = [1,3,2,4]

In R: my_vector <- c(1,3,2,4)

Introduction to Bash Scripting

Creating an array in Bash

Creation of a numerical-indexed can be done in two ways in Bash.

1. Declare without adding elements

declare -a my_first_array

2. Create and add elements at the same time

my_first_array=(1 2 3)

Remember - no spaces around equals sign!

Introduction to Bash Scripting

Be careful of commas!

Commas are not used to separate array elements in Bash:

This is not correct:

my_first_array=(1, 2, 3)

This is correct:

my_first_array=(1 2 3)
Introduction to Bash Scripting

Important array properties

  • All array elements can be returned using array[@]. Though do note, Bash requires curly brackets around the array name when you want to access these properties.
my_array=(1 3 5 2)
echo ${my_array[@]}
1 3 5 2
  • The length of an array is accessed using #array[@]
echo ${#my_array[@]}
4
Introduction to Bash Scripting

Manipulating array elements

Accessing array elements using square brackets.

my_first_array=(15 20 300 42)
echo ${my_first_array[2]}
300
  • Remember: Bash uses zero-indexing for arrays like Python (but unlike R!)
Introduction to Bash Scripting

Manipulating array elements

Set array elements using the index notation.

my_first_array=(15 20 300 42 23 2 4 33 54 67 66)

my_first_array[0]=999
echo ${my_first_array[0]}
999
  • Remember: don't use the $ when overwriting an index such as $my_first_array[0]=999, as this will not work.
Introduction to Bash Scripting

Manipulating array elements

Use the notation array[@]:N:M to 'slice' out a subset of the array.

  • Here N is the starting index and M is how many elements to return.
my_first_array=(15 20 300 42 23 2 4 33 54 67 66)
echo ${my_first_array[@]:3:2}
42 23
Introduction to Bash Scripting

Appending to arrays

Append to an array using array+=(elements).

For example:

my_array=(300 42 23 2 4 33 54 67 66)

my_array+=(10) echo ${my_array[@]}
300 42 23 2 4 33 54 67 66 10
Introduction to Bash Scripting

(Not) appending to arrays

What happens if you do not add parentheses around what you want to append? Let's see.

For example:

my_array=(300 42 23 2 4 33 54 67 66)
my_array+=10
echo ${my_array[@]}
30010 42 23 2 4 33 54 67 66

The string 10 will just be added to the first element. Not what we want!

Introduction to Bash Scripting

Associative arrays

  • An associative array
    • Similar to a normal array, but with key-value pairs, not numerical indexes
    • Similar to Python's dictionary or R's list
    • Note: This is only available in Bash 4 onwards. Some modern macs have old Bash! Check with bash --version in terminal

In Python:

my_dict = {'city_name': "New York", 'population': 14000000}

In R:

my_list = list(city_name = c('New York'), population = c(14000000))
Introduction to Bash Scripting

Creating an associative array

You can only create an associative array using the declare syntax (and uppercase -A).

You can either declare first, then add elements or do it all on one line.

  • Surround 'keys' in square brackets, then associate a value after the equals sign.
    • You may add multiple elements at once.
Introduction to Bash Scripting

Associative array example

Let's make an associative array:

declare -A city_details # Declare first

city_details=([city_name]="New York" [population]=14000000) # Add elements
echo ${city_details[city_name]} # Index using key to return a value
New York
Introduction to Bash Scripting

Creating an associative array

Alternatively, create an associative array and assign in one line

  • Everything else is the same
declare -A city_details=([city_name]="New York" [population]=14000000)

Access the 'keys' of an associative array with an !

echo ${!city_details[@]} # Return all the keys
city_name population
Introduction to Bash Scripting

Let's practice!

Introduction to Bash Scripting

Preparing Video For Download...