Bash Arrays
A Bash array is a variable that can hold multiple values, each stored at a specific index. Instead of creating many separate variables, arrays let us group related data together.
Example use cases:
- List of servers
- Packages to install
- Users to create
- Files or directories to process
Creating an Array
Basic Array Syntax
servers=("web1" "web2" "web")
Each value is separated by a space and wrapped in parentheses.
You can also create arrays like this:
servers[1]="web1"
servers[2]="web2"
servers[3]="web3"
Accessing Array Elements
To access an element, use its index:
echo ${servers[0]}
Output:
web1
Always use ${} when accessing array values.
Accessing All Elements
To print all elements in an array:
echo ${servers[@]}
Or Loop through them:
for server in "${servers[@]}"; do
echo "Server: $servers"
done
Output:
Server: web1
Server: web2
Server: web3
Getting the Array Length
Knowing how many items are in an array is crucial for automation.
echo ${#servers[@]}
Adding # before variable_name which is servers.
This prints the number of elements in the array.
Output:
3
Adding Elements to an Array
You can append new values like this:
servers+=("asd1")
Now the array contains:
web1 web2 web3 asd1
Removing Elements from an Array
Use unset to remove a specific index:
unset servers[1]
This removes the value but does not reindex the array automatically.
To see Indexes:
echo ${!servers[@]}
Output:
0 2 3
Looping with Indexes (Platform Engineer Pattern)
Sometimes you need both the index and the value:
#!/bin/bash
servers=( "web1" "web2" "web3" "asd1" )
for i in "${!servers[@]}"; do
echo "Index $i ā ${servers[$i]}"
done
Output:
Index 0 -> web1
Index 1 -> web2
Index 2 -> web3
Index 3 -> asd1
To Review
Arrays + Functions (Very Important)
Platform engineers often combine functions and arrays.
#!/bin/bash
# Script: arrays_functions.sh
# Purpose: Demonstrate how to use Bash arrays inside functions
# Define a function to print package installation messages
print_packages() {
for pkg in "$@"; do
echo "Installing $pkg..."
done
}
# Create an array of packages
packages=("nginx" "docker" "git")
# Call the function and pass all array elements
print_packages "${packages[@]}"
# Bonus: Add a new package and call the function again
packages+=("htop")
echo ""
echo "After adding a new package:"
print_packages "${packages[@]}"
How It Works
packages=("nginx" "docker" "git") - creates an array of package names.
print_packages() - a function that loops through all arguments ($@) and prints a message.
print_packages "${packages[@]}" - passes all array elements to the function.
packages+=("htop") - appends a new package to the array.
Calling print_packages again shows the updated array.
Expected Output:
Installing nginx...
Installing docker...
Installing git...
After adding a new package:
Installing nginx...
Installing docker...
Installing git...
Installing htop...
Today, I learned about Bash arrays and how they can store multiple values in a single variable, which makes scripts more organized. I also explored using arrays with functions, but Iām having a hard time understanding how $@ passes array elements to a function. I practiced looping through the array inside a function, but I still feel a bit unsure about combining arrays and functions in my scripts. This lesson showed me that I need more practice to fully understand this important concept for automation tasks.
Tomorrow, I will review and practice Bash arrays to become more comfortable using them. I also plan to explore more ways arrays are used in real automation tasks, so I can get closer to being platform engineer ready.
Top comments (0)