Bash Array – Declare, Initialize and Access – Examples

Bash Array

Bash Array – In this Bash Tutorial, we shall learn how to declare, initialize and access one dimensional Bash Array, with the help of examples.

Syntax

Following is the syntax of how to declare an array, initialize it, access elements of array and finally display the value of each element of array if required.

where

ARRAY_NAMEName that you would give to the array. Rules of naming a variable in bash hold for naming array as well
ELEMENT_NNth element of array

Elements could be accessed with index using curly braces as shown in the syntax.

 

Example – Bash Array

Following is an example Bash Script in which we shall create an array names, initialize it, access elements of it and display all the elements of it.

 

Example – Accessing elements of Array using For Loop

In this example, we shall learn to access elements of an array iteratively using Bash For Loop.

 

Conclusion :

In this Bash Tutorial, we have learnt how to declare, initialize and access one dimensional Bash Array, with the help of examples.