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 Linux Command Line & Bash Scripting Mastery lesson on CoddyKit. This is 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 Linux Command Line & Bash Scripting Mastery learning path, and your progress syncs across the web and the CoddyKit app. The Linux Command Line & Bash Scripting Mastery 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]}" # 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.
Learn Bash 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
- 22
- Lessons
- 88
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. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Command Line & Bash Scripting Mastery course, upgrade to CoddyKit PRO. The Linux Command Line & Bash Scripting Mastery 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 Linux Command Line & Bash Scripting Mastery 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 Linux Command Line & Bash Scripting Mastery?
No prior experience is required. Linux Command Line & Bash Scripting Mastery 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 Linux Command Line & Bash Scripting Mastery lesson?
Yes. Every Linux Command Line & Bash Scripting Mastery 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