Bash Scripting

Bash Arrays Explained Simply: Beginner’s Guide with Examples

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.

What is an Array in Bash?

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:

  • Indexed arrays – values are stored in numbered positions, starting from 0.
  • Associative arrays – values are stored with named keys, which makes them easier to identify.

Creating Arrays

Indexed Arrays

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  

Associative Arrays

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  

Accessing Array Values

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  

Finding Array Length

echo ${#fruits[@]}    # number of items → 3  
echo ${#fruits[0]}    # length of first word → 5 (apple)  

Looping Through Arrays

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  

Adding and Removing Items

Add new items

fruits+=("dragonfruit")
echo ${fruits[@]}

Output:

apple banana cherry dragonfruit  

Remove a single item

unset fruits[1]
echo ${fruits[@]}

Output:

apple cherry dragonfruit  

Remove the entire array

unset fruits
echo ${fruits[@]}

Output:

(nothing — the array is empty)  

Conclusion

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.

  • Use indexed arrays when order matters.
  • Use associative arrays when names or labels make things easier.
  • You can mix different types of values, add new ones, or remove them whenever needed.
0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Bash For Loop Examples Explained Simply for Beginners

If you are new to Bash scripting or Linux shell scripting, one of the most…

11 hours ago

How Does a Firewall Work Step by Step

How Does a Firewall Work Step by Step? What Is a Firewall and How Does…

2 days ago

ROADTools: The Modern Azure AD Exploration Framework

ROADTools is a powerful framework designed for exploring and interacting with Microsoft Azure Active Directory…

5 days ago

How to Enumerate Microsoft 365 Groups Using PowerShell and Python

Microsoft 365 Groups (also known as M365 Groups or Unified Groups) are at the heart…

5 days ago

SeamlessPass: Using Kerberos Tickets to Access Microsoft 365

SeamlessPass is a specialized tool designed to leverage on-premises Active Directory Kerberos tickets to obtain…

6 days ago

PPLBlade: Advanced Memory Dumping and Obfuscation Tool

PPLBlade is a powerful Protected Process Dumper designed to capture memory from target processes, hide…

6 days ago