Bash Array
An array holds a list of values in one variable. Create it with parentheses: fruits=(apple banana cherry).
Indexes start at 0. ${fruits[@]} gives all items and ${#fruits[@]} gives the number of items.
Create and read an array
Save this as array.sh:
#!/bin/bash
fruits=(apple banana cherry)
echo "First: ${fruits[0]}"
echo "All: ${fruits[@]}"
echo "Count: ${#fruits[@]}"
fruits+=(mango)
echo "Count now: ${#fruits[@]}"
Run it:
./array.sh
First: apple
All: apple banana cherry
Count: 3
Count now: 4
Loop over an array
colors=(red green blue)
for c in "${colors[@]}"; do echo "Color: $c"; done
Color: red
Color: green
Color: blue