Bash Arrays Explained
If you’re learning Bash scripting, one of the most useful features you’ll come across is the array. Arrays make it easy to store and work with multiple values under one name instead of creating a separate variable for each.
This guide will explain arrays in simple terms with examples and outputs so even beginners can follow along.
An array in Bash is a variable that can store multiple values together. These values can be strings, numbers, or even a mix of both. Bash doesn’t care about data type — it treats everything as text.
There are two main types of arrays in Bash:
You can create them one by one or all at once:
# One by one
fruits[0]="apple"
fruits[1]="banana"
fruits[2]="cherry"
# All at once
numbers=(10 20 30 40)
# Mixed values
mixed=("apple" 42 "hello.txt")
Output:
echo ${fruits[@]} → apple banana cherry
echo ${numbers[@]} → 10 20 30 40
echo ${mixed[@]} → apple 42 hello.txt
These work with keys instead of numbers (requires Bash 4+):
declare -A colors
colors[red]="#FF0000"
colors[blue]="#0000FF"
colors[green]="#00FF00"
declare -a
makes a normal indexed array (numbered positions), while declare -A
makes an associative array (named keys, works in Bash 4+). So, declare -A colors
means: “create a variable called colors
and treat it as an associative array.”
Output:
echo ${colors[red]} → #FF0000
echo ${!colors[@]} → red blue green
echo ${colors[@]} → #FF0000 #0000FF #00FF00
Once values are stored, here’s how you use them:
# Get one item
echo ${fruits[1]} # banana
echo ${numbers[2]} # 30
echo ${mixed[0]} # apple
# Get all items
echo ${fruits[@]} # apple banana cherry
# Get keys in associative arrays
echo ${!colors[@]} # red blue green
# Get values in associative arrays
echo ${colors[@]} # #FF0000 #0000FF #00FF00
echo ${#fruits[@]} # number of items → 3
echo ${#fruits[0]} # length of first word → 5 (apple)
Loop through all values:
for item in "${fruits[@]}"; do
echo $item
done
Output:
apple
banana
cherry
Loop with index numbers:
for i in "${!fruits[@]}"; do
echo "Index $i = ${fruits[$i]}"
done
Output:
Index 0 = apple
Index 1 = banana
Index 2 = cherry
Loop through associative arrays:
for key in "${!colors[@]}"; do
echo "$key = ${colors[$key]}"
done
Output:
red = #FF0000
blue = #0000FF
green = #00FF00
fruits+=("dragonfruit")
echo ${fruits[@]}
Output:
apple banana cherry dragonfruit
unset fruits[1]
echo ${fruits[@]}
Output:
apple cherry dragonfruit
unset fruits
echo ${fruits[@]}
Output:
(nothing — the array is empty)
Arrays in Bash are a simple but powerful way to organize data. You can store words, numbers, or filenames all in one place, and manage them easily using loops, keys, and commands.
An array in Bash is a single variable that can hold many values at once, like a list.
Yes, Bash arrays can hold text, numbers, and even a mix of both.
Indexed arrays use numbers as positions (0, 1, 2…), while associative arrays use names or keys (like “red” or “blue”).
declare -a
and declare -A
mean? declare -a
creates a normal indexed array, and declare -A
creates an associative array (which works only in Bash version 4 or newer).
Run this command:bash --version
Associative arrays require Bash 4 or newer.
Docker is a powerful open-source containerization platform that allows developers to build, test, and deploy…
Docker is one of the most widely used containerization platforms. But there may come a…
Introduction Google Dorking is a technique where advanced search operators are used to uncover information…
Introduction In cybersecurity and IT operations, logging fundamentals form the backbone of monitoring, forensics, and…
What is Networking? Networking brings together devices like computers, servers, routers, and switches so they…
Introduction In the world of Open Source Intelligence (OSINT), anonymity and operational security (OPSEC) are…