Array Variables in Bash, How to Use, With Examples

Bash Array

We’ve covered using variables in Bash previously – this article will explain Bash array variables and provide some example usage.

What is an Array

An array is a type of variable that can hold multiple values. It’s a list of values you can loop through and perform operations on each individual value.

For example, you might want to perform an action on a list of files. By storing that list as an array, you can loop through the file names in it and perform the action on each one.

Arrays are indexed, with the position of each item in the array being represented by a number starting at 0.

Bash arrays do not require that array elements be indexed contiguously – or adjacent without gaps – you can have values at array positions 0, 3, 17 with no values in between, for example.

You can find out more about Bash arrays at the Linux Documentation Project.

Creating Bash Arrays

Bash arrays can contain any type of bash variable – paths, strings, numbers – even other arrays.

Why You Should Wrap File Paths in Strings in Your Shell Scripts

There are several methods for declaring arrays, outlined below.

Indirect Declaration

Arrays can be declared indirectly by assigning the array element’s value – the array will be created along with the array element with the given value.

ARRAYNAME[INDEX]=VALUE

For example:

#!/bin/bash
peopleArray[3]="Tom"

The array peopleArray will be created automatically (indirectly) when a value in the array is assigned – in this case, the value “Tom” is assigned to index 3 in the newly created peopleArray.

Direct Declaration

The declare command can also be used to define an array:

declare -a ARRAYNAME

For example:

declare -a peopleArray

This would create an empty array called peopleArray.

Creating via Compound Assignment

This is the method you’ll probably use most. It creates an array with the values already assigned.

ARRAYNAME=(value1 value2 ...)

This assigns the value of the array to the variable name. The array values are contained in () (standard brackets) and separated by spaces. Values containing spaces should be quoted.

For example:

peopleArray=("Tom", "Bill", "Theodore Baker Sr.")

Assigning Values to an Array

Adding a New Value to the End of an Array

To add a value to an array, assign the value with no index specified:

ARRAYNAME[]=VALUE

For example:

peopleArray[]="Tony"

Replacing Value in an Array

To replace a value, overwrite the value at the current value’s index.

For example, if you have an array with a value at index 7 which you wish to replace:

peopleArray[7]="Tony"

Deleting Value in an Array

Delete a value from an array by using the unset command and specifying the array index:

unset ARRAYNAME[INDEX]

For example:

unset peopleArray[1]

…will delete the second value in the array peopleArray.

Accessing Array Values

To access the value in an array, you must use curly braces – otherwise, Bash will not interpret the brackets containing the array index correctly:

echo ${ARRAYNAME[INDEX]}

For example:

#!/bin/bash
peopleArray=("Tom", "Bill", "Theodore Baker Sr.")
echo ${peopleArray[0]}

…will output:

Tom

Whereas if the last line were:

echo $peopleArray[0]

Looping Arrays

The most common thing you’ll do with arrays is loop through them to perform operations on each element. We’re way ahead of things and have already covered this article.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *