declare sting array bash code example

Example 1: bash array

# create an array - just assign first value
arrayname[0]='VALUE'

# define an array
declare -a arrayname=(element1 element2 element3)

# get array element at index 
${arrayname[2]}

# get all array elements
${arrayname[@]}

# get array length
${#arrayname[@]}

Example 2: how to loop through every value in array bash

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done