It's sometimes the case that we'd like to store a set of multiple values in one variable, where we don't know how many there might be at the time we start the program, especially sets of filenames. Unfortunately, it's not safe to store a set of filenames in a simple string variable, even when separated by newlines, because a newline is itself a valid character for filenames – there's no safe choice of delimiter for the data, because we can't use null bytes in shell words.
Fortunately, Bash provides arrays to allow us to do this:
bash$ fruits=('apple' 'banana' 'cherry')
Note that we separate the parts of the array only with spaces, not commas, and we do not quote the parentheses; they are part of the syntax. With this array defined, we can expand members of it with numeric suffixes in square brackets, starting from 0:
bash$ printf '%s\n' "${fruits[0]}" apple bash$ printf '%s\n' "${fruits[2]}" cherry
We can reference values indexed from the end of the array with negative subscripts, starting with -1:
bash$ printf '%s\n' "${fruits[-1]}" cherry
We can expand all of the array elements using @ as a special subscript value:
bash$ printf '%s\n' "${fruits[@]}" apple banana cherry
We will be using arrays a great deal in the next chapter when discussing loops and conditionals.
One convenient property of arrays is that after expanding them with the special @ index, we can apply parameter expansion operations to every element:
bash$ printf '%s\n' "${fruits[@]^^}" APPLE BANANA CHERRY