AnyCompiler

Arrays

An array is declared with parentheses: name=(value1 value2 ...). ${name[0]} reads one element by index, ${name[@]} expands to every element, and ${#name[@]} gives the element count — the # prefix means "length of" in parameter expansion generally, covered in full in a later lesson.

fruits=("apple" "banana" "cherry")
echo "${fruits[0]}"
echo "${fruits[@]}"
echo "${#fruits[@]}"
Output
apple
apple banana cherry
3

Next: associative arrays — Bash's key-value map type.

Practice exercise

Given colors=("red" "green" "blue"), print the first element and then the total count of elements.