Skip to content

07. Bash Arrays

In Bash scripting, an array is a variable that can hold multiple values at the same time. Instead of storing a single value like a normal variable, arrays allow you to store a list of values and access them using an index.

Bash arrays are zero-indexed, meaning the first element starts at index 0.

Example concept:

Index:   0        1        2
Array: [Apple] [Banana] [Orange]

1. Declaring a Simple Bash Array

A Bash array can be declared by placing values inside parentheses.

Example Script

#!/bin/bash

# Declare array with elements
ARRAY=('Debian Linux' 'Redhat Linux' Ubuntu Linux)

# Get number of elements in array
ELEMENTS=${#ARRAY[@]}

# Loop through array and print elements
for (( i=0;i<$ELEMENTS;i++)); do
    echo ${ARRAY[${i}]}
done

Explanation

1. Declaring the Array

ARRAY=('Debian Linux' 'Redhat Linux' Ubuntu Linux)

This creates an array named ARRAY with four elements:

Index Value
0 Debian Linux
1 Redhat Linux
2 Ubuntu
3 Linux

Note:
The first two values are wrapped in quotes because they contain spaces.


2. Getting the Number of Elements

ELEMENTS=${#ARRAY[@]}

${#ARRAY[@]} returns the total number of elements in the array.

Example output:

4

3. Accessing Array Elements

${ARRAY[index]}

Example:

${ARRAY[0]} → Debian Linux
${ARRAY[1]} → Redhat Linux

4. Looping Through the Array

for (( i=0;i<$ELEMENTS;i++)); do
    echo ${ARRAY[${i}]}
done

This loop:

  1. Starts at index 0

  2. Runs until the last element

  3. Prints each array value


Output

Debian Linux
Redhat Linux
Ubuntu
Linux

2. Reading File Content into a Bash Array

Instead of defining array values inside the script, Bash can read values from a file and store them in an array.


Example Script

#!/bin/bash

# Declare array
declare -a ARRAY

# Link file descriptor 10 with stdin
exec 10<&0

# Replace stdin with file supplied as first argument
exec < $1

let count=0

while read LINE; do
    ARRAY[$count]=$LINE
    ((count++))
done

echo Number of elements: ${#ARRAY[@]}

# Print array contents
echo ${ARRAY[@]}

# Restore stdin
exec 0<&10 10<&-

Example Input File

bash.txt

Bash
Scripting
Tutorial
Guide

Running the Script

./bash-script.sh bash.txt

Output

Number of elements: 4
Bash Scripting Tutorial Guide

Explanation

1. Declaring an Array

declare -a ARRAY

This explicitly declares a variable as an array.


2. Redirecting Input from File

exec < $1

This replaces standard input (stdin) with the file provided as the first argument.

Example:

./script.sh bash.txt

Here $1 = bash.txt


3. Reading File Line by Line

while read LINE; do

This reads each line of the file one by one.


4. Storing Lines into the Array

ARRAY[$count]=$LINE

Each line from the file becomes an array element.

Example:

Index Value
0 Bash
1 Scripting
2 Tutorial
3 Guide

5. Incrementing the Index

((count++))

This increases the array index after each iteration.


6. Printing Number of Elements

${#ARRAY[@]}

Returns total array elements.

Output:

4

7. Printing Array Content

echo ${ARRAY[@]}

${ARRAY[@]} prints all elements of the array.

Output:

Bash Scripting Tutorial Guide

Key Bash Array Syntax

Syntax Description
ARRAY=(a b c) Declare array
${ARRAY[0]} Access element
${ARRAY[@]} All elements
${#ARRAY[@]} Number of elements
ARRAY[3]=value Assign element
unset ARRAY[2] Remove element

Summary

Bash arrays allow scripts to manage multiple values efficiently. Key capabilities include:

  • Declaring arrays

  • Accessing elements using indexes

  • Looping through arrays

  • Reading external data (such as files) into arrays

Arrays are commonly used in Bash scripts for:

  • Handling wordlists

  • Processing file data

  • Automating system tasks

  • Managing command outputs

Understanding arrays is essential for writing more structured and powerful Bash scripts.