How to Use Array in Linux Bash

Yahya Irmak Feb 02, 2024
How to Use Array in Linux Bash

Bash has an array structure (some languages call it a list). In this article, we will explain this structure. Later, we will examine the following topics in Bash scripting.

  • Array declaration
  • Access values in the array
  • Add new value to the array
  • Delete a value from the array

An array is a data structure holding similar data together. Each data in the array has an index, and the index of the first element is 0. We can access these data sequentially with a loop or individually using the data index. We can also change the array, add new values and delete existing values.

Using Array in Bash

Now that we know what an array is, we can examine its use in Bash.

Declare an Array

An array is declared by enclosing values in parentheses. There are only spaces between the values, no characters such as commas. An example array declaration is below.

arr=("black" "white" "red" "blue" "green")

We can use the following methods to declare an integer-valued array.

intArr=(1 2 3 4)
intArr=($(seq 1 4))
intArr=({1..4}) 

These examples declare an array containing the numbers 1 to 4.

Access an Array

There are many different ways to access data in an array. For example, we can print the entire array, access a single element using the index, or access each element in turn with a loop. Let’s explain each method one by one.

Access the Entire Array

We can use the [*] or the [@] commands to access all the values that an array holds. For example, let’s use these commands to print all the values in the arr array we defined above.

arr=("black" "white" "red" "blue" "green")
echo "With *: ${arr[*]}"
echo "With @: ${arr[@]}"

Access the entire array

Access an Element With Index

The index of the first element in the array is 0, and the indexes of the following elements increase sequentially. We can access the element by the element’s index number in the array. For example, we use the Bash code below to get the red value in the arr array.

arr=("black" "white" "red" "blue" "green")
echo "${arr[2]}"

Access the element with index

Access Array Elements With Loop

We can use the for loop to access all the elements of an array one by one. Below is the Bash script that will print each color in the arr array to the screen.

arr=("black" "white" "red" "blue" "green")
for color in ${arr[@]}; do
  echo $color
done

Access elements with loop

If we want to access these values together with the index number, we can use the code below.

arr=("black" "white" "red" "blue" "green")
for i in ${!arr[@]}; do
  echo "$i: ${arr[$i]}"
done

The ${!arr[@]} command returns the indexes of all values in the array. Then, the element is accessed using these indexes.

Access numbered elements with loop

Add New Element to an Array

To add a new element to the array, we can assign an index number to the new value or use the += operator.

arr=("black" "white" "red" "blue" "green")
echo "Before adding purple: ${arr[@]}"
arr[5]="purple" # or use arr+=("purple"), exactly same
echo "After adding purple: ${arr[@]}"

Add element to array

Delete an Element From an Array

We can use the unset command to delete any data in the array or even delete completely the array itself.

arr=("black" "white" "red" "blue" "green")
echo "Before removing white: ${arr[@]}"
unset arr[1]
echo "After removing white: ${arr[@]}"

Delete an element from array

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn

Related Article - Bash Array