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

Array Basics

In this page:

  1. Array Basics

Array Basics

Bash arrays store multiple values in a single variable, declared with name=(value1 value2 value3). Individual elements are accessed with ${name[index]}, using zero-based indexing. Arrays are one of the few compound data structures Bash offers natively.

Note: Always quote array expansions, like "${arr[@]}", to keep elements with spaces intact.

Example: Array Basics

bash
#!/bin/bash
fruits=("apple" "banana" "cherry")

echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"

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.