The stuff I do

Bash arrays

← Notes

Tags: [bash][array]

Initiate an array 🔗

myArray=( element_1 element_2 element_n )
myArray2=(
element_1
element_2
element_n
)

Access item by index 🔗

echo ${myArray[2]}

Iterate through array 🔗

To iterate over the elements:

for element in "${array[@]}"
do
echo "$element"
done

To get both the index and the value:

for index in "${!array[@]}"
do
echo "$index ${array[index]}"
done

Append element to array 🔗

myarray=()
myarray+=('foo')
myarray+=('bar')

← Notes

Back to top