Fundamentals 5 min read

Master Bash Arrays: Declaration, Access, Looping, and Real-World Uses

This guide explains how to declare, initialize, access, modify, iterate, measure length, and delete Bash arrays, and demonstrates practical scenarios such as file list handling, argument passing, and numeric calculations to boost script efficiency.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Bash Arrays: Declaration, Access, Looping, and Real-World Uses

Introduction

Bash arrays store multiple values in a single variable, enabling efficient handling of lists, batch file operations, and numeric calculations.

Declaring and Initializing Arrays

Two common approaches are supported:

Using parentheses to assign values directly:

# Declare and initialize an array
myArray=(value1 value2 value3)

Using declare -a and then assigning each index:

# Declare an empty indexed array
declare -a myArray
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"

Accessing Elements

Array elements are accessed by a zero‑based index:

#!/bin/bash
myArray=("apple" "banana" "cherry")
echo "First element: ${myArray[0]}"
echo "Second element: ${myArray[1]}"
echo "Third element: ${myArray[2]}"

Modifying Elements

Assign a new value to a specific index to replace an element:

#!/bin/bash
myArray=("apple" "banana" "cherry")
myArray[1]="blueberry"
echo "Modified array: ${myArray[@]}"

Iterating Over an Array

A for loop can traverse all elements:

#!/bin/bash
myArray=("apple" "banana" "cherry")
for element in "${myArray[@]}"; do
    echo "$element"
done

Getting Array Length

The expression ${#myArray[@]} returns the number of elements:

#!/bin/bash
myArray=("apple" "banana" "cherry")
length=${#myArray[@]}
echo "Array length: $length"

Removing Elements

Use unset to delete a single element or the entire array:

#!/bin/bash
myArray=("apple" "banana" "cherry")
# Delete the second element (index 1)
unset myArray[1]
echo "After deletion: ${myArray[@]}"
# Delete the whole array
unset myArray

Practical Applications

File List Processing

Collect all files in the current directory and iterate over them:

#!/bin/bash
files=(* )
for file in "${files[@]}"; do
    echo "$file"
done

Argument Passing

Store command‑line arguments in an array and process each one:

#!/bin/bash
args=("$@")
for arg in "${args[@]}"; do
    echo "$arg"
done

Numeric Computation

Sum the values of a numeric array using arithmetic expansion:

#!/bin/bash
numbers=(1 2 3 4 5)
sum=0
for number in "${numbers[@]}"; do
    sum=$((sum + number))
done
echo "Sum: $sum"

Conclusion

Arrays are a fundamental data structure in Bash scripting. Mastering declaration, initialization, element access, modification, iteration, length retrieval, and element removal enables the creation of flexible, maintainable scripts for a wide range of automation tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxArrays
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.