Arrays — Managing Collections of Data in Bash Scripts¶
Arrays allow Bash scripts to store multiple values under a single variable name. Instead of creating many individual variables, arrays organize related data into indexed collections, making scripts cleaner, easier to maintain, and more scalable. Arrays are commonly used in automation scripts, server management, cloud provisioning, deployment pipelines, and system administration to process lists of servers, files, users, packages, and other collections of data.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand Bash arrays
- Create indexed arrays
- Access array elements
- Iterate through arrays
- Add and remove elements
- Determine array length
- Use associative arrays
- Apply arrays in production scripts
Prerequisites¶
Complete:
- Modules 1–9
- Module 10 Lessons 1–4
Why Learn Arrays?¶
Imagine checking the status of multiple servers.
Without arrays:
Managing many variables becomes difficult.
Using an array:
The script becomes much cleaner and easier to scale.
What is an Array?¶
An array stores multiple values inside a single variable.
Example:
Each item has an index.
Creating an Array¶
Syntax:
Example:
Accessing Array Elements¶
Display the first element.
Output:
Display another element.
Output:
Display All Elements¶
Output:
Array Indexes¶
Bash arrays begin at index 0.
Example:
Array Length¶
Display the number of elements.
Output:
Length of One Element¶
Output:
Because:
contains three characters.
Add Elements¶
Append a new value.
Display:
Modify Elements¶
Display:
Remove Elements¶
Display:
Loop Through an Array¶
Output:
Loop Using Indexes¶
Reading User Input into an Array¶
Example input:
Display:
Associative Arrays¶
Associative arrays use keys instead of numeric indexes.
Enable:
Assign values.
Retrieve values.
Output:
Display Keys¶
Display Values¶
Common Commands¶
Create array.
Display all elements.
Display one element.
Display length.
Remove element.
Real Production Examples¶
Store servers.
Restart services.
Package installation.
Production Perspective¶
Arrays are widely used in:
- Server automation
- Cloud deployments
- Kubernetes administration
- Backup automation
- Configuration management
- Package installation
- Monitoring scripts
- CI/CD pipelines
Arrays simplify the management of large collections of related data.
Hands-on Lab¶
Task 1¶
Create an array.
Task 2¶
Display all elements.
Task 3¶
Display the first element.
Task 4¶
Display the array length.
Task 5¶
Add an element.
Task 6¶
Loop through the array.
Task 7¶
Create an associative array.
Task 8¶
Display associative array values.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
ARRAY=() | Create array | Store data |
${ARRAY[@]} | Display all elements | Reporting |
${ARRAY[index]} | Access one element | Configuration |
${#ARRAY[@]} | Array length | Validation |
unset | Remove element | Cleanup |
declare -A | Create associative array | Key-value configuration |
Common Array Mistakes¶
| Mistake | Solution |
|---|---|
| Starting indexes at 1 | Bash arrays start at 0 |
Forgetting quotes around "${ARRAY[@]}" | Quote array expansion |
Using associative arrays without declare -A | Declare first |
| Confusing array length with string length | Use the correct syntax |
| Treating arrays like strings | Access individual elements properly |
Production Troubleshooting Scenario¶
Scenario
A monitoring script checks multiple servers.
Without arrays:
Improved:
SERVERS=("web01" "web02" "db01")
for SERVER in "${SERVERS[@]}"
do
if ping -c1 "$SERVER" >/dev/null
then
echo "$SERVER is reachable"
else
echo "$SERVER is unreachable"
fi
done
Adding a new server requires changing only one line in the array.
Best Practices¶
- Use arrays for related collections of data.
- Quote
"${ARRAY[@]}"when iterating. - Use descriptive array names.
- Prefer associative arrays for key-value mappings.
- Keep array operations simple and readable.
- Validate array contents before processing.
Common Mistakes¶
❌ Starting array indexes at 1 instead of 0.
✅ Prefer 0 rather than starting array indexes at 1.
❌ Forgetting quotes around array expansion.
✅ Remember to quotes around array expansion.
❌ Accessing elements that do not exist.
✅ Avoid this mistake: accessing elements that do not exist.
❌ Using multiple variables instead of arrays.
✅ Prefer arrays rather than using multiple variables.
❌ Forgetting to declare associative arrays.
✅ Remember to to declare associative arrays.
Interview Questions¶
Beginner¶
- What is an array?
- How do you create an array in Bash?
- How do you access the first element?
- How do you display all array elements?
Intermediate¶
- How do you determine the length of an array?
- What is the difference between indexed and associative arrays?
- How do you iterate through an array?
- How do you remove an array element?
Architect Level¶
- How would you use arrays to automate deployments across multiple servers?
- Why are associative arrays useful in infrastructure automation?
- How do arrays improve the maintainability of large Bash scripts?
Summary¶
In this lesson, you learned:
- Indexed arrays
- Associative arrays
- Accessing array elements
- Looping through arrays
- Adding and removing elements
- Array length
- Production scripting techniques
Arrays allow Bash scripts to efficiently manage collections of related data. They reduce repetitive code, improve readability, and simplify automation tasks involving multiple files, users, servers, or configuration values.
Key Takeaways¶
- Arrays store multiple values in a single variable.
- Bash array indexes start at 0.
- Use
${ARRAY[@]}to access all elements. - Use
${#ARRAY[@]}to determine array length. - Use
declare -Afor associative arrays. - Arrays make Bash scripts more scalable and easier to maintain.
What's Next?¶
Input — Accepting User Input in Bash Scripts
You'll explore:
- Reading user input
- Command-line arguments
- Interactive prompts
- Input validation
- Password input
- Default values
- Production scripting examples
By the end of the lesson, you'll be able to build interactive Bash scripts that safely accept, validate, and process user input for real-world automation tasks.