HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Arrays

Arrays

Array creation, accessing elements, and element count.

Simple enough for a 5-year-old to understand

Create an array and access elements

Elements are separated by spaces (not commas).

Example code
1
colors=(red purple green)
An array with 3 elements.
DataAn array with 3 elements.
2
echo ${colors[0]}
First element: a
Output / InputFirst element: a
3
echo ${#colors[@]}
Number of elements: 3
Output / InputNumber of elements: 3
DataOutput / Input
💡 Hint: ${arr[@]} all elements, ${#arr[@]} number of elements.

About this topic

Bash arrays allow you to store multiple values in a single variable. They are the right tool for things like file lists, argument lists, or command options; keeping the same data in a single space-separated text is error-prone.

Arrays are specific to Bash and do not exist in the POSIX shell. Therefore, the shebang line of a script using arrays must be bash; when run with sh, it produces a syntax error.

The most critical point in arrays is the expansion syntax. A special syntax is required to get all elements of the array CORRECTLY, and the quoted and unquoted versions of this syntax behave differently. A wrong choice breaks elements containing spaces by splitting them.

What you will learn

  • Defining arrays and adding elements
  • Accessing a single element and all elements
  • Getting the array length
  • The difference between quoted and unquoted expansion
  • Iterating over an array

When to use?

Use an array if you will carry multiple filenames or command options; a single space-separated text will definitely break when values contain spaces.

To read command output into an array line by line, the array mode of the read command is used; assigning the output directly produces a single text, not an array.

Common mistakes

Wrong

files=("a.txt" "my file.txt")
for f in ${files[@]}; do
  echo "$f"
done

Unquoted expansion results in word splitting: "my file.txt" is treated as two separate elements and the loop runs three times. There is no error message, you silently get the wrong result. Putting the expansion in double quotes protects the elements from breaking.

Correct

files=("a.txt" "my file.txt")
for f in "${files[@]}"; do
  echo "$f"
done

Wrong

files=$(ls *.txt)
echo "${#files[@]}"

Normal assignment does not create an array, it produces a single text; that's why the length query returns 1. Creating an array requires parenthesis assignment or the array mode of the read command. Also, parsing ls output breaks with spaced names, shell expansion is safer.

Correct

files=(*.txt)
echo "${#files[@]}"

Wrong

arr=(10 20 30)
echo "${arr}"
echo "${arr[3]}"

Syntax without an index gives ONLY the first element, not the whole array; 10 is printed to the screen. On the third line, the index is out of bounds and Bash does not throw an error, it returns an empty value. Indices start at zero, so in a three-element array, the largest valid index is two.

Correct

arr=(10 20 30)
echo "${arr[@]}"
echo "${arr[2]}"

Frequently asked questions

What is the difference between the at-sign and the asterisk?

Inside double quotes, the at-sign makes each element a SEPARATE word, while the asterisk combines them all into a single word. In loops and command arguments, the at-sign is almost always desired; the asterisk is only correct when you want to merge elements and produce a single text.

Are there associative arrays?

They have existed since Bash version four: they are defined with declare and use text as keys. Since the Bash that comes with macOS by default is version three, it won't work there; if portability is required, this restriction must be taken into account.

How do I add an element to an array?

A value in parentheses is added with the compound assignment operator; this grows the array towards the end. It is also possible to assign to a specific index and the indices in between remain empty — Bash arrays can be sparse, so the length and the largest index are not the same thing.

Summary

Arrays are specific to Bash, they do not work with sh.

To get all elements, the at-sign expansion is used within double quotes.

Syntax without an index gives the first element, not the whole array.

Out-of-bounds index does not throw an error, it returns an empty value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code