bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Bash/Scripting
Bash•Scripting

Bash Arrays

Working with Arrays in Bash

This section explains how to create and manipulate arrays in Bash scripts.

Arrays allow you to store multiple values in a single variable, making data management easier.

Creating Arrays

To create an array in Bash, use the following syntax.

Arrays can store multiple values, and each value is indexed starting from zero:

Example: Create an Array

my_array=("value1" "value2" "value3")

Use descriptive names for arrays to indicate their purpose.

Accessing Array Elements

To access elements in a Bash array, use the index of the element.

The index is specified in square brackets:

Example: Access Array Elements

echo ${my_array[0]}

Modifying Array Elements

You can modify elements in a Bash array by specifying the index and assigning a new value:

Example: Modify Array Elements

my_array[1]="new_value"

Previous

Bash Functions

Next

Bash crontab Command - Schedule Tasks

This chapter

Overview
10

Lessons

20m

Read time

1. Basic Bash Syntax2. Bash Script3. Bash Variables4. Bash Data Types5. Bash Operators6. Bash If...Else7. Bash Loops8. Bash Functions9. Bash Arrays10. Bash crontab Command - Schedule Tasks

On this page

Working with Arrays in BashCreating ArraysExample: Create an ArrayAccessing Array ElementsExample: Access Array ElementsModifying Array ElementsExample: Modify Array Elements