Bash Arrays

Bash Arrays

In Bash scripting, arrays are used to store multiple values in a single variable. Understanding how to define, manipulate, and perform operations on arrays is crucial for effective shell scripting.

Defining Arrays

Arrays in Bash can be defined using parentheses with elements separated by spaces.


 # Define an array
array=("value1" "value2" "value3")

In this example, array is defined with three elements: 'value1', 'value2', and 'value3'.

Accessing Array Elements

Array elements can be accessed using their indices, starting from 0.


# Define an array
array=("value1" "value2" "value3")
# Access array elements
echo "First element: ${array[0]}"
echo "Second element: ${array[1]}"

In this example, the first and second elements of array are accessed using their indices.

Accessing array elements in Bash

Adding Elements to an Array

Elements can be added to an array using the += operator.


 # Define an array
array=("value1" "value2" "value3")
# Add elements to an array
array+=("value4" "value5")
echo "Array after adding elements: ${array[@]}"

In this example, 'value4' and 'value5' are added to array using the += operator.

Adding elements to an array in Bash

Removing Elements from an Array

Elements can be removed from an array using the unset command followed by the index of the element to remove.


 # Define an array
array=("value1" "value2" "value3")
# Remove an element from an array
unset array[1]
echo "Array after removing second element: ${array[@]}"

In this example, the second element of array is removed using the unset command.

Removing elements from an array in Bash

Iterating Over an Array

Array elements can be iterated over using a for loop.


 # Define an array
array=("value1" "value2" "value3")
# Iterate over array elements
for element in "${array[@]}"; do
    echo "$element"

In this example, a for loop is used to iterate over each element in array and print it.

Iterating over an array in Bash

Finding the Length of an Array

The length of an array can be determined using ${#array[@]}.


 # Define an array
array=("value1" "value2" "value3")
# Find the length of an array
echo "Length of the array: $length"

In this example, the length of array is determined using ${#array[@]} and printed.

Finding the length of an array in Bash

Associative Arrays

Associative arrays are collections of key-value pairs and are supported in Bash version 4.0 and above.


 # Define an associative array
declare -A assoc_array

# Access elements in the associative array
echo "Name: ${assoc_array["name"]}"
echo "Age: ${assoc_array["age"]}"

In this example, the associative array assoc_array is defined using the declare -A syntax. Elements are accessed using their keys.

Defining and accessing elements in an associative array in Bash


Working with arrays in Bash is essential for handling collections of data in shell scripts. Understanding how to define, access, add, remove, iterate over, and determine the length of arrays, as well as work with associative arrays, can help you manage and manipulate data effectively in your scripts.