$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

Indexed Arrays

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:25

Creating an array

You create an indexed array with parentheses on the right side of an assignment. Each word inside the parentheses becomes one element. Quoting still applies, so a value with a space must be quoted to remain one element. The elements are numbered from zero, and the array can be sparse, which means some indices can be missing.

BASH2 lines
fruits=(apple "red delicious" cherry)
echo "${fruits[1]}"

The second line prints red delicious, which is a single element. Reading an element always needs the braces, because $fruits[1] means the scalar $fruits followed by the text [1]. The expansion ${fruits[1]} is the form to use.

Expanding all elements

The two expansions "${fruits[@]}" and "${fruits[*]}" look similar but behave differently. The quoted @ form expands to one word per element, keeping each element intact even if it contains spaces. The quoted * form joins all elements into a single word, separated by the first character of IFS. In almost every script you want the @ form.

BASH3 lines
fruits=(apple "red delicious" cherry)
printf "[%s]\n" "${fruits[@]}"
printf "[%s]\n" "${fruits[*]}"

The first printf prints three bracketed lines, one for each element, including [red delicious]. The second prints one line, [apple red delicious cherry], because the star form joined everything into a single argument. Forgetting the quotes or using the star form is the most common array bug.

Length, indices, and appending

${#fruits[@]} gives the number of elements, and ${#fruits[1]} gives the length of one element. The expansion ${!fruits[@]} lists the indices that are set, which matters for sparse arrays. You can add an element to the end with +=, and you can set any index directly.

BASH5 lines
fruits=(apple "red delicious" cherry)
fruits+=(mango)
fruits[10]=kiwi
echo "count=${#fruits[@]}"
echo "indices=${!fruits[@]}"

The output is count=5 and indices=0 1 2 3 10. The array holds five elements, but index 10 is the highest, and indices 4 to 9 are not set. A loop written as for ((i=0; i<${#fruits[@]}; i++)) would miss kiwi and would try to read unset elements. Loop over the indices or over the values instead.

Note

Indexed arrays in bash start at zero, and negative indices count from the end. ${fruits[-1]} is the last element, which was added in bash 4.3. Older shells do not support it, so use it only where you control the bash version.

Iterating over an array safely

The standard way to loop over an array is for item in "${fruits[@]}". The quotes keep each element together, and the loop sees exactly the elements that were stored. To loop with indices, use for i in "${!fruits[@]}" and read ${fruits[$i]} inside the body. This form still works after you remove an element with unset, which leaves a hole in the index sequence.

BASH3 lines
fruits=(apple "red delicious" cherry)
unset "fruits[1]"
for i in "${!fruits[@]}"; do echo "$i: ${fruits[$i]}"; done

The loop prints 0: apple and 2: cherry. The index 1 is missing, and the loop skips it without a special case. Notice that the subscript in unset is quoted, so the brackets are not treated as a glob.

Arrays from command output

Arrays are a good way to collect the lines produced by a command. Bash 4 and later provide mapfile, also called readarray, which reads standard input into an array with one element per line. The -t option removes the trailing newline from each element.

BASH2 lines
mapfile -t lines < <(printf "%s\n" alpha beta gamma)
echo "${#lines[@]} lines, second is ${lines[1]}"

The output is 3 lines, second is beta. The process substitution <(...) feeds the output of the command to mapfile as if it were a file, and it runs in the current shell, so the array is still set afterward.

Hands-on: build and inspect an array

Run the block below and compare the output with your expectation before you run it. Then change the quotes in the last line and see how the output shifts. The lab for this unit asks you to write a script that does the same thing and leaves its output in a file.

BASH3 lines
tools=(ls "grep -r" awk)
echo "count=${#tools[@]}"
printf "<[%s]>\n" "${tools[@]}"

The output shows three elements, and the element grep -r stays one element, because the quoted expansion keeps it together.

Terminal lab · 2 steps · real Linux in your browser

Build and index an array

Write an array script with quoted expansion, then a sparse array script with a gap in its indices.

01
Quote the array expansion
Create arr.sh in /workspace. It declares an array with three elements, one of which contains a space. It prints the count and then each element in its own bracketed line: ``bash cat > arr.sh <<'EOF' fruits=(apple "red delicious" cherry) echo "count=${#fruits[@]}" printf "[%s]\n" "${fruits[@]}" EOF `` Run bash arr.sh and check that red delicious stays on one line.
Show hint for step 1
Use the quoted at-sign form inside the printf call so each element stays one argument.
02
Make the array sparse
Create sparse.sh in /workspace. It builds the same three fruits, then sets index 10 to kiwi, and prints the element count followed by the list of set indices: ``bash cat > sparse.sh <<'EOF' fruits=(apple "red delicious" cherry) fruits[10]=kiwi echo "${#fruits[@]}" echo "${!fruits[@]}" EOF ``
Show hint for step 2
The index list comes from the expansion with an exclamation mark before the name. It prints the set indices, not the values.
Check yourself Question 1 of 4
Concept Check Counting elements recall

After fruits=(a 'b c' d), what does ${#fruits[@]} expand to?

Recap & Key Takeaways

An indexed array is created with name=(...), each word or quoted string is one element, and elements are read with the braces form such as ${name[1]}.
Use "${name[@]}" to expand every element as its own word, because the star form joins all elements into one word.
${#name[@]} counts the elements and ${!name[@]} lists the set indices, which matters for sparse arrays and after unset.
mapfile -t reads lines of standard input into an array, which is a safe way to collect command output.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue