How to use Arrays in Bash

In this article, I will introduce how to declare, assign values to and access an array in Bash, as well as how to find the length of an array and check if an object is included in the array.

1. Declaring an Array

An array can be declared and assigned like this. Objects are assigned by index.

#!/bin/bash

FRUITS[0]="Apple"
FRUITS[1]="Banana"
FRUITS[2]="Kiwi"
FRUITS[3]="Grape"

In Bash, arrays can also be defined in the following form.

#!/bin/bash

FRUITS=("Apple", "Banana", "Kiwi", "Grape")

2. Accessing elements with Index

You can get access to elements of an array using ${ARRAY_NAME[Index]}.

Here is an example to print each element.

#!/bin/bash

FRUITS[0]="Apple"
FRUITS[1]="Banana"
FRUITS[2]="Kiwi"
FRUITS[3]="Grape"

echo "First Index: ${FRUITS[0]}"
echo "Second Index: ${FRUITS[1]}"
echo "Third Index: ${FRUITS[2]}"
echo "Fourth Index: ${FRUITS[3]}"

Output:

$ bash example.sh
First Index: Apple
Second Index: Banana
Third Index: Kiwi
Fourth Index: Grape

3. Accessing all the elements

To get all elements of an array, you can use with like ${ARRAY_NAME[*]} or ${ARRAY_NAME[@]}, which will returns all elements.

As in the following example, ${ARRAY_NAME[* or @]} will return all elements of the array.

#!/bin/bash

FRUITS[0]="Apple"
FRUITS[1]="Banana"
FRUITS[2]="Kiwi"
FRUITS[3]="Grape"

echo "All Items: ${FRUITS[*]}"
echo "All Items: ${FRUITS[@]}"

Output:

$ bash example.sh
All Items: Apple Banana Kiwi Grape
All Items: Apple Banana Kiwi Grape

4. Accessing elements with for loop

You can use a loop to traverse all elements of an array and perform some operation.

The following example uses a loop to add all elements of an array.

#!/bin/bash

arr=(10 20 30 40 50)

sum=0

for i in ${arr[@]}
do
    sum=`expr $sum + $i`
done

echo $sum

Output:

$ bash example.sh
150

5. Get the length of Array

You can get the length of an array by entering a # in front of ARRAY_NAME[@], which means all elements of the array, such as ${#ARRAY_NAME[*]}" or ${#ARRAY_NAME[@]}".

Here is an example of finding the length of an array.

#!/bin/bash

FRUITS[0]="Apple"
FRUITS[1]="Banana"
FRUITS[2]="Kiwi"
FRUITS[3]="Grape"

echo "Array length: ${#FRUITS[*]}"
echo "Array length: ${#FRUITS[@]}"

Output:

Array length: 4
Array length: 4

6. Check if a certain element exists in an array

You can check if a certain element exists in an array by iterating through all the elements of the array with an if statement.

The following example is to check if a certain element exists in an array. A function called in_array is defined to determine the inclusion.

#!/bin/bash

function in_array {
    ARRAY=$2
    for e in ${ARRAY[*]}
    do
        if [[ "$e" == "$1" ]]
        then
            return 0
        fi
    done

    return 1
}


FRUITS[0]="Apple"
FRUITS[1]="Banana"
FRUITS[2]="Kiwi"
FRUITS[3]="Grape"

# Check if it contains 'Orange'
if in_array "Orange" "${FRUITS[*]}"
then
    echo "Found: Orange"
else
    echo "Not found: Orange"
fi

# Check if it contains 'Kiwi'
if in_array "Kiwi" "${FRUITS[*]}"
then
    echo "Found: Kiwi"
else
    echo "Not found: Kiwi"
fi

Output:

$ bash example.sh
Not found: Orange
Found: Kiwi
codechachaCopyright ©2019 codechacha