Mark Lucernas
2020-08-09

Array

Basics

Declaring bash array

  • declare -a array_name

Initializing bash array

  • array_name=( element_1 element_2 element_n )

Access an individual element

  • echo "${array[0]}" – Access the first index of an array
  • echo "${array[-1]}"' – Access the last index of an array

Iterate over the elements of an array

for element in "${array[@]}"; do
  echo "$element"
done

With index and value

for index in "${!array[@]}"; do
  echo "$index ${array[index]}"
done

Deleting values of an index

  • unset "array[1]"

Adding values to an index

  • array[42]="Some string" – Bash arrays are not contiguous and can add elements in an arbitrary index

Get length of an array

  • echo "${#array[@]}"

Appending to Array

Appending array element by using shorthand operator

# Declare a string array
arrVar=("element 1" "element 2" "element 3" "element 4")

# Add new element at the end of the array
arrVar+=("element 5")

# Iterate the loop to read and print each array element
for value in "${arrVar[@]}"; do
  echo $value
done

Appending array element by defining the last index

# Declare a string array
arrVar=("element 1" "element 2" "element 3" "element 4")

# Add new element at the end of the array
arrVar[${#arrVar[@]}]="element 5"

# Iterate the loop to read and print each array element
for value in "${arrVar[@]}"; do
  echo $value
done

Appending array element by using bracket

# Declare a string array
arrVar=("element 1" "element 2" "element 3" "element 4")

# Add new element at the end of the array
arrVar=(${#arrVar[@]} "element 5")

# Iterate the loop to read and print each array element
for value in "${arrVar[@]}"; do
  echo $value
done

Append multiple elements at the end of the array

# Declare a string array
arrVar1=("element 1" "element 2" "element 3")
arrVar2=("element 4" "element 5" "element 6")

# Add new element at the end of the array
arrVar=(${#arrVar1[@]} ${#arrVar2[@]})

# Iterate the loop to read and print each array element
for value in "${arrVar[@]}"; do
  echo $value
done

Ref:

Split String Into Array

Method 1:

string="Paris, France, Europe"

# Split string into array delimited by ', '
IFS=', ' read -r -a array <<< "$string"

# Iterate over the elements
for element in "${array[@]}"; do
  echo "$element"
done

Method 2: (recommended)

# 'arr' as the variable for the array
readarray -td '' arr < <(awk '{ gsub(/, /,"\0"); print; }' <<<"$string, "); unset 'arr[-1]'; declare -p arr;

NOTE: Must read this to understand the comparison about read and readarray when splitting string into array.

Ref:

Detecting if Variable is an Array

if declare -p ARRAY 2> /dev/null | grep -q '^declare \-a'; then
  echo "$ARRAY is an array"
else
  echo "$ARRAY is not an array"
fi

Ref:

Check Array Contains a Value

if [[ " ${array[@]} " =~ " ${value} " ]]; then
    # whatever you want to do when array contains value
fi

if [[ ! " ${array[@]} " =~ " ${value} " ]]; then
    # whatever you want to do when array doesn't contain value
fi

Small function with looping

containsElement () {
  local arr match="$1"
  shift
  for arr; do [[ "$arr" == "$match" ]] && return 0; done
  return 1
}

Ref:

Passing An Array as Function Argument

#!/bin/bash

my_function() {
  arr=("$@")

  # Loop over array
  for i in "${arr[@]}"; do
    echo "$i"
  done
}

array=("one" "two" "three")
my_function "${array[@]}"

Ref:


Resources