Python Arrays: Indexed Arrays, Associative Maps & Slicing
Bash supports both 0-indexed numerical arrays (`arr=(a b c)`) and string-keyed Associative Arrays (`declare -A map`). Mastering array expansion (`"${arr[@]}"` with double quotes) guarantees that elements with spaces are never accidentally split.
"An indexed array is like a numbered row of lockers (0, 1, 2, ...); an associative array is like a set of mailboxes labeled with people's names ("alice", "bob", "database_host")."
Deep Dive: How It Works
Indexed Array Syntax: `servers=("web-01" "web-02" "db-01")`. Access by index: `${servers[0]}`. All elements: `"${servers[@]}"`. Total count: `${#servers[@]}`.
Append Element: `servers+=("cache-01")`.
Associative Arrays (Hash Maps): `declare -A server_ports=(["http"]=80 ["https"]=443 ["ssh"]=22)`. Access keys with `${!server_ports[@]}`.
Syntax Blueprint
FRUITS=("Apple" "Banana" "Cherry")
echo "First: ${FRUITS[0]}"
echo "Total: ${#FRUITS[@]}"
for fruit in "${FRUITS[@]}"; do
echo "$fruit"
done
declare -A USER=(["name"]="Alex" ["role"]="admin")
echo "Role: ${USER["role"]}"Always wrap ${ARRAY[@]} in double quotes to preserve elements containing spaces.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using `${arr[*]}` without quotes instead of `"${arr[@]}"`.Why it happens: `${arr[*]}` merges all array elements into a single giant string joined by the first character of IFS.
How to fix: Always use `"${arr[@]}"` when iterating.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create and Iterate an Array
Create an array `ITEMS=("alpha" "beta" "gamma")`.
Print total elements using `${#ITEMS[@]}` and echo "Items count: ${#ITEMS[@]}".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.