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.
Working with Arrays in Bash is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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]}" # cherryAll Elements at Once
Use ${array[@]} to expand to every element. This is what you loop over.
echo "${fruits[@]}" # apple banana cherryArray Length
Get the number of elements with ${#array[@]}.
echo "${#fruits[@]}" # 3Iterating 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"
doneAdding 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 bananaSlicing Arrays
Extract a sub-range with ${array[@]:start:count}.
echo "${fruits[@]:0:2}" # first twoAssociative 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]}" # 30Iterating Keys and Values
Loop over keys with ${!array[@]} and fetch each value by key.
for name in "${!ages[@]}"; do
echo "$name is ${ages[$name]}"
doneQuick 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.
Frequently asked questions
Is the “Working with Arrays in Bash” lesson free?
Yes — the full text of “Working with Arrays in Bash” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- First Bash Script & Shebang
- Variables and User Input
- Conditional Statements (if, else, elif)
- Working with Arrays in Bash