Arrays in Linux: A Complete Guide for Beginners

Learn how to use arrays in Linux with simple examples. This beginner’s guide covers creation, access, and management of indexed and associative arrays in shell scripts.
code
linux
Author

Steven P. Sanderson II, MPH

Published

May 16, 2025

Keywords

Programming, arrays in Linux, Linux arrays, bash arrays, Linux shell scripting, associative arrays in Linux, indexed arrays in Linux, bash scripting arrays, Linux array examples, array operations in bash, Linux array tutorial, how to use arrays in Linux shell scripting, creating and accessing arrays in bash, associative vs indexed arrays in Linux, practical examples of arrays in Linux, beginner’s guide to arrays in Linux scripting

Authors Note: As I write this series, I am also learning myself. While I try to be accurate and clear in this series, I may make mistakes or present solutions that could be improved upon. I welcome constructive feedback. As I discover better approaches or need to correct information, I will update the content accordingly. Thank you for reading.

What Are Arrays in Linux?

Arrays are variables that can hold multiple values under one name. Instead of creating separate variables like fruit1, fruit2, and fruit3, you can have a single array named fruits that holds all these values. This makes your scripts cleaner and more organized.

Linux Bash supports two types of arrays:

  1. Indexed arrays: Use numbers as indexes (starting at 0)
  2. Associative arrays: Use text strings as indexes (like “name” or “color”)

Arrays are useful when you need to work with lists of related items such as filenames, user information, or configuration settings.


Creating Indexed Arrays

There are several ways to create an indexed array in Linux:

Method 1: Assign values directly

fruits[0]="apple"
fruits[1]="banana"
fruits[2]="cherry"

Method 2: Create all at once

fruits=("apple" "banana" "cherry")

Method 3: Using declare

declare -a fruits
fruits=("apple" "banana" "cherry")

The declare -a command tells Bash that this variable is an indexed array.


Accessing Array Elements

To access a specific element in an array, use the index number inside square brackets:

echo ${fruits[0]}  # Prints: apple
echo ${fruits[1]}  # Prints: banana
echo ${fruits[2]}  # Prints: cherry

Notice that we use ${} around the array name and index. This is important for correct access.

To access all elements at once, use the @ symbol:

echo ${fruits[@]}  # Prints: apple banana cherry

To find out how many elements are in an array:

echo ${#fruits[@]}  # Prints: 3

Modifying Arrays

Adding Elements

Add elements to an array by specifying an index:

fruits[3]="date"

Or add to the end of the array:

fruits+=("elderberry")

Removing Elements

Remove an element using the unset command:

unset 'fruits[1]'  # Removes banana

The quotes are important to prevent pathname expansion issues.

Important: Removing an element doesn’t reindex the array. The indexes of other elements stay the same.

After removing the element at index 1:

echo ${fruits[@]}  # Prints: apple cherry date elderberry

To remove the entire array:

unset fruits

Looping Through Arrays

A common task is to process each element in an array. Use a for loop:

for fruit in "${fruits[@]}"; do
  echo "I like $fruit"
done

This will print:

I like apple
I like cherry
I like date
I like elderberry

You can also loop through array indexes:

for i in "${!fruits[@]}"; do
  echo "Element $i: ${fruits[$i]}"
done

This will print:

Element 0: apple
Element 2: cherry
Element 3: date
Element 4: elderberry

Notice that index 1 is missing because we removed it earlier.


Associative Arrays

Associative arrays use text strings instead of numbers as indexes. This lets you organize data in a more meaningful way.

Creating Associative Arrays

To create an associative array, you must first declare it using the -A option:

declare -A person

Then you can assign values:

person[name]="John"
person[age]=30
person[city]="New York"

Or create and initialize in one line:

declare -A person=([name]="John" [age]=30 [city]="New York")

Accessing Associative Array Elements

Access elements using the key:

echo ${person[name]}  # Prints: John
echo ${person[age]}   # Prints: 30
echo ${person[city]}  # Prints: New York

To get all keys in an associative array:

echo ${!person[@]}  # Prints: name age city

Looping Through Associative Arrays

Loop through all keys and values:

for key in "${!person[@]}"; do
  echo "$key: ${person[$key]}"
done

This will print:

name: John
age: 30
city: New York

Practical Example: Shopping List Manager

Let’s build a simple shopping list manager using arrays:

#!/bin/bash

# Initialize shopping list (indexed array)
shopping_list=("milk" "bread" "eggs")
echo "Initial shopping list: ${shopping_list[@]}"

# Initialize prices (associative array)
declare -A prices
prices[milk]=3.99
prices[bread]=2.49
prices[eggs]=3.29

# Add a new item
shopping_list+=("cheese")
prices[cheese]=4.99
echo "Added cheese to list"
echo "Updated shopping list: ${shopping_list[@]}"

# Remove an item (bread)
for i in "${!shopping_list[@]}"; do
  if [ "${shopping_list[$i]}" = "bread" ]; then
    unset 'shopping_list[$i]'
    unset 'prices[bread]'
    echo "Removed bread from list"
    break
  fi
done

# Calculate total cost
total=0
echo "Your shopping list:"
for item in "${shopping_list[@]}"; do
  price=${prices[$item]}
  total=$(echo "$total + $price" | bc)
  echo "- $item: \$$price"
done

echo "Total cost: \$$total"

Output:

Initial shopping list: milk bread eggs
Added cheese to list
Updated shopping list: milk bread eggs cheese
Removed bread from list
Your shopping list:
- milk: $3.99
- eggs: $3.29
- cheese: $4.99
Total cost: $12.27

This example shows how you can use both indexed arrays (for the shopping list) and associative arrays (for the prices) together in a practical script.


Common Mistakes to Avoid

  1. Forgetting that array indices start at 0, not 1

    days=("Mon" "Tue" "Wed")
    echo ${days[1]}  # Prints: Tue (not Mon)
  2. Not using ${} syntax when accessing arrays

    # Wrong:
    echo $days[0]  # Doesn't work correctly
    
    # Correct:
    echo ${days[0]}
  3. Not quoting array elements when deleting

    # Correct way:
    unset 'days[1]'
  4. Assuming empty values remove array contents

    days[1]=""  # This doesn't delete the element, just makes it empty
  5. Trying to access non-existent elements

    echo ${days[10]}  # Won't cause an error, but prints nothing if it doesn't exist

Your Turn!

Now it’s time to practice! Try creating a script that:

  1. Creates an array called colors with at least 5 colors
  2. Adds two more colors to the array
  3. Removes one specific color
  4. Creates an associative array that maps each color to its hex code
  5. Prints all colors with their hex codes
Click here for Solution!
#!/bin/bash

# Create an array of colors
colors=("red" "blue" "green" "yellow" "purple")
echo "Initial colors: ${colors[@]}"

# Add two more colors
colors+=("orange")
colors+=("black")
echo "After adding colors: ${colors[@]}"

# Remove green
for i in "${!colors[@]}"; do
  if [ "${colors[$i]}" = "green" ]; then
    unset 'colors[$i]'
    echo "Removed green"
    break
  fi
done

# Recreate array to reindex (optional)
colors=("${colors[@]}")
echo "Updated colors: ${colors[@]}"

# Create associative array for hex codes
declare -A hex_codes
hex_codes[red]="#FF0000"
hex_codes[blue]="#0000FF"
hex_codes[yellow]="#FFFF00"
hex_codes[purple]="#800080"
hex_codes[orange]="#FFA500"
hex_codes[black]="#000000"

# Print all colors with hex codes
echo "Colors with hex codes:"
for color in "${colors[@]}"; do
  echo "- $color: ${hex_codes[$color]}"
done

Output:

Initial colors: red blue green yellow purple
After adding colors: red blue green yellow purple orange black
Removed green
Updated colors: red blue yellow purple orange black
Colors with hex codes:
- red: #FF0000
- blue: #0000FF
- yellow: #FFFF00
- purple: #800080
- orange: #FFA500
- black: #000000

Quick Takeaways

  • Arrays in Linux store multiple values in a single variable
  • Use indexed arrays when order matters (elements accessed by number)
  • Use associative arrays when you need named keys
  • Array indexes start at 0, not 1
  • Always use ${} syntax when accessing array elements
  • Loop through arrays with for item in "${array[@]}" syntax
  • Access all array elements with ${array[@]}
  • Count array elements with ${#array[@]}
  • Add to arrays with array+=(new_value) or array[index]=value
  • Remove elements with unset 'array[index]'

Working with Array Slices

You can access a subset of array elements using slice notation:

fruits=("apple" "banana" "cherry" "date" "elderberry")

# Get 2 elements starting at index 1
echo ${fruits[@]:1:2}  # Prints: banana cherry

# Get all elements starting at index 2
echo ${fruits[@]:2}    # Prints: cherry date elderberry

Error Handling with Arrays

When working with arrays, you might encounter errors if you try to access non-existent elements or indexes. Here are some common error scenarios and how to handle them:

# Try to access non-existent index
shopping_list=("apples" "bread" "eggs")
item=${shopping_list[10]}  # No error, but variable is empty

# Check if an index exists before accessing
if [[ -n "${shopping_list[10]+x}" ]]; then
  echo "Item exists"
else
  echo "Item doesn't exist"  # This will be printed
fi

# Try to remove non-existent item
if [[ " ${shopping_list[@]} " =~ " cookies " ]]; then
  # Remove cookies
  # ...
else
  echo "Cannot remove cookies - item not in list"  # This will be printed
fi

Practical Use Case: Task List

Let’s see another practical example where arrays can be useful - a simple task manager:

#!/bin/bash

# Initialize task list with status
declare -A tasks
tasks["clean room"]="pending"
tasks["do laundry"]="completed"
tasks["buy groceries"]="pending"

# Display all tasks with status
echo "Task List:"
for task in "${!tasks[@]}"; do
  echo "- $task: ${tasks[$task]}"
done

# Update task status
tasks["clean room"]="completed"

echo -e "\nUpdated Task List:"
for task in "${!tasks[@]}"; do
  echo "- $task: ${tasks[$task]}"
done

# Count pending tasks
pending_count=0
for task in "${!tasks[@]}"; do
  if [ "${tasks[$task]}" = "pending" ]; then
    pending_count=$((pending_count + 1))
  fi
done

echo -e "\nYou have $pending_count pending tasks left."

Output:

Task List:
- clean room: pending
- do laundry: completed
- buy groceries: pending

Updated Task List:
- clean room: completed
- do laundry: completed
- buy groceries: pending

You have 1 pending tasks left.

This example shows how associative arrays can be used to track the status of various tasks, which is much more convenient than using separate variables.


FAQs

1. Can I mix different types of data in an array?

Yes, you can store strings, numbers, and even command outputs in the same array:

mixed=(42 "hello" $(date))

2. How do I check if an element exists in an array?

You can loop through the array and compare each element:

if [[ " ${array[@]} " =~ " $search_term " ]]; then
  echo "Found it!"
fi

3. Can I sort an array in Bash?

Yes, you can sort an array by creating a new array:

sorted_array=($(for i in "${array[@]}"; do echo "$i"; done | sort))

4. What’s the maximum size of an array in Bash?

Bash doesn’t have a set limit on array size, but very large arrays might slow down your script due to memory constraints.

5. How do I append one array to another?

You can combine arrays like this:

array1=("a" "b" "c")
array2=("d" "e" "f")
combined=("${array1[@]}" "${array2[@]}")

References

  1. GNU Bash Manual: Arrays - https://www.gnu.org/software/bash/manual/html_node/Arrays.html
  2. Advanced Bash-Scripting Guide: Arrays - https://tldp.org/LDP/abs/html/arrays.html
  3. Linux Documentation Project: Bash Guide for Beginners - https://tldp.org/LDP/Bash-Beginners-Guide/html/

Happy Coding! 🚀

Arrays in Linux

You can connect with me at any one of the below:

Telegram Channel here: https://t.me/steveondata

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

Mastadon Social here: https://mstdn.social/@stevensanderson

RStats Network here: https://rstats.me/@spsanderson

GitHub Network here: https://github.com/spsanderson

Bluesky Network here: https://bsky.app/profile/spsanderson.com

My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ

You.com Referral Link: https://you.com/join/EHSLDTL6