Array

noun

say it: “uh-RAY” /əˈreɪ/

A variable that keeps a list of values in numbered slots, counted from 0, so each item stays whole and can be read by its number.

What is an array in bash?

An array keeps a list in one variable, each item in a numbered slot counted from 0. ${songs[0]} reads one item, ${#songs[@]} counts them, and "${songs[@]}", in double quotes, is every item, each title in one piece.

songs=("Highway Tea" "Paper Lanterns" "Window Down")
echo "${songs[0]}"     # Highway Tea
echo "${#songs[@]}"    # 3
for song in "${songs[@]}"; do echo "Now playing: $song"; done
# Now playing: Highway Tea   (then the other two, one a line)

In a sentence

  • “The shuffler keeps every song title in an array called songs.”
  • “Each title in the array stays in one piece, spaces and all, when the loop reads it.”

Level: Journeyman · More Unix terms at this level

Updated

In other subjects