codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐧

Linux & Bash

40 / 60 topics
39Advanced Scripting40Bash Arrays41Bash Associative Arrays42Advanced Functions43Advanced Script Debugging44Script Optimization45Automation with Scripts46Script Integration47Script Logging48Error Handling49Script Performance50Parallel Processing51Remote Execution52Configuration Management53Script Monitoring54Automation Tools55Continuous Integration56Script Deployment57Script Security58Script Audit59Optimization Tips60Advanced Debugging
Tutorials/Linux & Bash/Bash Arrays
🐧Linux & Bash

Bash Arrays

Updated 2026-05-15
10 min read

Bash Arrays

Introduction

Arrays are a powerful feature in Bash that allow you to store multiple values in a single variable. They can be used for various complex operations, such as processing lists of items, performing batch operations, and more. In this tutorial, we will explore how to declare, manipulate, and use arrays in Bash scripts.

Concept

In Bash, an array is essentially a list of strings, where each string is an element of the array. Arrays can be indexed starting from 0, similar to many other programming languages. You can access individual elements using their index or iterate over all elements in the array.

Declaring Arrays

You can declare arrays in several ways:

  1. Using parentheses:

    fruits=("apple" "banana" "cherry")
    
  2. Using square brackets:

    fruits=(["0"]="apple" ["1"]="banana" ["2"]="cherry")
    
  3. Appending elements:

    fruits=()
    fruits+=("apple")
    fruits+=("banana")
    fruits+=("cherry")
    

Accessing Array Elements

You can access individual elements of an array using their index:

echo ${fruits[0]}  # Output: apple

To access all elements, you can use the @ or * symbol:

echo ${fruits[@]}  # Output: apple banana cherry
echo ${fruits[*]}  # Output: apple banana cherry

Array Length

You can find the length of an array using the ${#array[@]} syntax:

echo ${#fruits[@]}  # Output: 3

Examples

Let's explore some practical examples to understand how arrays work in Bash.

Example 1: Basic Array Operations

#!/bin/bash

# Declare an array
fruits=("apple" "banana" "cherry")

# Print all elements
echo "All fruits: ${fruits[@]}"

# Add a new fruit
fruits+=("date")
echo "After adding date: ${fruits[@]}"

# Remove the first element
unset fruits[0]
echo "After removing apple: ${fruits[@]}"
Output
All fruits: apple banana cherry  
After adding date: apple banana cherry date  
After removing apple: banana cherry date

Example 2: Iterating Over Arrays

You can use a for loop to iterate over all elements in an array:

#!/bin/bash

# Declare an array
fruits=("apple" "banana" "cherry")

# Iterate over the array
for fruit in "${fruits[@]}"; do
  echo "I like $fruit"
done
Output
I like apple  
I like banana  
I like cherry

Example 3: Associative Arrays

While not covered in this section, Bash also supports associative arrays, which allow you to use strings as indices. You can learn more about them in the "Bash Associative Arrays" section.

What's Next?

In the next tutorial, we will explore Bash Associative Arrays, which provide a way to map keys to values similar to dictionaries or hash maps in other programming languages. This feature is particularly useful for more complex data structures and operations.

Stay tuned for more advanced topics in Bash scripting!


PreviousAdvanced ScriptingNext Bash Associative Arrays

Recommended Gear

Advanced ScriptingBash Associative Arrays