← Back to Bash Course | Chapter 2: Variables & Types | Lesson 12 of 13

Introduction to Arrays

An array is a single variable that can hold a whole list of values instead of just one, each accessible by a numeric position.

Creating an Indexed Array

You create an array literal by listing values inside parentheses separated by spaces, e.g. fruits=(apple banana cherry). Bash automatically assigns indices starting at 0, so apple is at index 0, banana at index 1, and so on.

Example: Creating an Indexed Array

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"

Accessing All Elements

${arr[@]} expands to every element of the array as separate words, which is what you want when looping over the contents. Using just $arr or ${arr} without an index only refers to element 0, a common beginner mistake.

Example: Accessing All Elements

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "All fruits: ${fruits[@]}"
echo "Just index 0 (via \$fruits): $fruits"

Array Length

${#arr[@]} gives the total number of elements currently stored in the array, which is useful for loop bounds or validation. This is different from ${#arr} or ${#arr[0]}, which give the character length of the first element's string.

Example: Array Length

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "Number of fruits: ${#fruits[@]}"
echo "Length of first element string: ${#fruits[0]}"

Appending to an Array

New elements can be added to the end of an array using the += operator with a parenthesized value list. This grows the array without needing to know its current length or rebuild it from scratch.

Warning: A full deep dive into arrays -- associative arrays, slicing, and more -- comes later in its own chapter.

Example: Appending to an Array

bash
#!/bin/bash
fruits=(apple banana)
fruits+=(cherry)
fruits+=(date fig)
echo "Fruits now: ${fruits[@]}"
echo "Count: ${#fruits[@]}"
Common Mistakes
  1. Forgetting the parentheses when creating an array literal; fruits=apple banana does not create an array, it tries to run banana as a command with fruits=apple as an environment prefix.
  2. Using $fruits (no index) and expecting all elements; that only gives you the first element (index 0) -- you need ${fruits[@]} for the whole array.
  3. Assuming array indices are always contiguous starting at 0 with no gaps; Bash indexed arrays are actually sparse, so removing an element leaves a gap rather than shifting later elements down.
Chapter Summary
  • Create an indexed array with arr=(one two three); indices start at 0 by default.
  • Access a single element with ${arr[0]}; access all elements with ${arr[@]}.
  • ${#arr[@]} gives the number of elements currently in the array.
  • Bash arrays are sparse -- unset elements leave gaps in the index sequence rather than being removed and renumbered.

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.