← Back to Bash Course | Chapter 6: Arrays | Lesson 2 of 10

Indexed Arrays in Depth

Indexed arrays store an ordered list of values under a single variable name, each one accessible by a numeric position starting at 0.

Declaring and Assigning Indexed Arrays

Indexed arrays can be declared explicitly with declare -a arrname or simply created by assigning a parenthesized list of values, arr=(a b c). Either way, Bash automatically numbers the elements starting at index 0.

Example: Declaring and Assigning Indexed Arrays

bash
#!/bin/bash
declare -a numbers
numbers=(10 20 30)
echo "First: ${numbers[0]}, Second: ${numbers[1]}, Third: ${numbers[2]}"

Assigning to Specific Indices

Individual elements can be set directly by index, arr[5]=value, without needing to have previously assigned the indices before it. This can produce a sparse array, where some numeric indices between the lowest and highest simply don't exist.

Warning: The array has only 2 real elements even though the highest index is 5, because indices 1-4 were never assigned.

Example: Assigning to Specific Indices

bash
#!/bin/bash
declare -a grid
grid[0]="start"
grid[5]="end"
echo "grid[0] = ${grid[0]}"
echo "grid[5] = ${grid[5]}"
echo "Number of actual elements: ${#grid[@]}"

Modifying an Existing Element

An existing array element can be overwritten just like a normal variable, by assigning a new value to its specific index. This does not affect any other elements in the array.

Example: Modifying an Existing Element

bash
#!/bin/bash
colors=(red green blue)
colors[1]="yellow"
echo "Updated array: ${colors[@]}"

Printing All Elements and Their Count

${arr[@]} expands to all elements as separate words, and ${#arr[@]} gives the total count of elements currently present. These two expansions are the backbone of nearly every array-processing pattern in Bash.

Example: Printing All Elements and Their Count

bash
#!/bin/bash
planets=(Mercury Venus Earth Mars)
echo "All planets: ${planets[@]}"
echo "Total count: ${#planets[@]}"
Common Mistakes
  1. Assuming array indices must be assigned consecutively starting at 0; you can assign directly to any index, e.g. arr[10]=x, which creates a sparse array with gaps in between.
  2. Forgetting ${arr[@]} vs ${arr[*]} differ when quoted, the same subtlety as $@ vs $* for positional parameters.
  3. Trying to get an array's length with ${#arr} instead of ${#arr[@]}; the former only gives the character length of the first element.
Chapter Summary
  • Declare explicitly with declare -a arr or just assign directly, arr=(a b c).
  • Individual elements can be assigned or read by explicit index: arr[2]=value, ${arr[2]}.
  • Arrays can be sparse -- assigning to arr[100] doesn't create 100 empty slots before it.
  • ${arr[@]:offset:length} and other expansions covered later in this chapter all build on the indexed array fundamentals here.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.