DevOps Bootcamp · Lesson

Working with Arrays in Bash

Store and process lists of values. Learn to declare arrays, access and iterate elements, get the length, and use associative arrays as key-value maps.

Lesson 4 of 413 steps

Working with Arrays in Bash is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Why Arrays?

A normal variable holds one value. An array holds many — perfect for lists of files, names, or arguments you want to loop over in a script.

Declaring an Array

Create an indexed array by listing values in parentheses, separated by spaces.

fruits=(apple banana cherry)

Accessing Elements

Index elements with ${array[i]}, starting at 0. The braces are required.

echo "${fruits[0]}"   # apple
echo "${fruits[2]}"   # cherry

All Elements at Once

Use ${array[@]} to expand to every element. This is what you loop over.

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

Array Length

Get the number of elements with ${#array[@]}.

echo "${#fruits[@]}"   # 3

Iterating with a for Loop

Loop over an array by expanding all elements. Quoting "${arr[@]}" keeps values with spaces intact.

for f in "${fruits[@]}"; do
  echo "Fruit: $f"
done

Adding Elements

Append to an array with +=. You can also assign a specific index directly.

fruits+=(mango)
fruits[10]="kiwi"

Removing Elements

unset deletes an element by index. Note this leaves a gap rather than reindexing.

unset 'fruits[1]'   # removes banana

Slicing Arrays

Extract a sub-range with ${array[@]:start:count}.

echo "${fruits[@]:0:2}"   # first two

Associative Arrays

Bash 4+ supports key-value maps. Declare with declare -A, then index by string keys.

declare -A ages
ages[alice]=30
ages[bob]=25
echo "${ages[alice]}"   # 30

Iterating Keys and Values

Loop over keys with ${!array[@]} and fetch each value by key.

for name in "${!ages[@]}"; do
  echo "$name is ${ages[$name]}"
done

Quick Check

Test your understanding.

Recap

You learned Bash arrays: declare with (), index with ${arr[i]}, expand all with ${arr[@]}, count with ${#arr[@]}, loop, append with +=, slice, and use declare -A for associative key-value maps.

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Working with Arrays in Bash” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Working with Arrays in Bash”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Working with Arrays in Bash”?

Store and process lists of values. Learn to declare arrays, access and iterate elements, get the length, and use associative arrays as key-value maps. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Working with Arrays in Bash” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. First Bash Script & Shebang
  2. Variables and User Input
  3. Conditional Statements (if, else, elif)
  4. Working with Arrays in Bash
← Back to DevOps Bootcamp