Using arrays

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')
Arrays are not specified at all as part of POSIX shell. They are a feature specific to Bash.

Note that we separate the parts of the array only with spaces, not ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.