Array Index
noun
say it: “uh-RAY IN-deks” /əˈreɪ ˈɪndɛks/
The number of a slot in an array, counted from 0, used to read or change the item in that slot.
What is an array index in bash?
An array index is the number of a slot in an array. Bash counts from 0, so the index says how many slots from the start: the first song is 0 and the third is 2. ${songs[-1]} is the last item, and "${!songs[@]}" lists every index, handy for numbering a list.
songs=("Highway Tea" "Paper Lanterns" "Window Down")
echo "${songs[2]}" # Window Down
echo "${songs[-1]}" # Window Down
for i in "${!songs[@]}"; do echo "$((i + 1)). ${songs[$i]}"; done
# 1. Highway Tea
# 2. Paper Lanterns
# 3. Window Down
In a sentence
- “The third song sits at array index 2, because bash counts from 0.”
- “Sam looped over every array index to print the songs as a numbered list.”
Level: Journeyman · More Unix terms at this level
Updated