Composite Type: Indexed Arrays

Learn how to declare and use indexed arrays.

We'll cover the following

Indexed arrays

The Bourne Shell has scalar variables only. The interpreter stores them as strings in the memory. Working with such variables is inconvenient in some cases, so developers added arrays to the Bash language. When do we need an array?

Strings have a serious limitation. When we write a value to the scalar variable, it is a single unit. For example, we save a list of file names in the variable called files. We separate them by spaces. As a result, the files variable stores a single string from the Bash point of view—which can lead to errors.

The root cause of the problem comes from the POSIX standard. It allows any characters in file names except the null character (NULL). NULL means the end of a file name. The same character means the end of a string in Bash. Therefore, a string variable can contain NULL at the end only. It turns out that we have no reliable way to separate file names in a string. We cannot use NULL, but any other delimiter character can occur in the names.

We cannot reliably process the results of the ls utility because of the delimiter problem. The utility cannot use NULL as a separator for the names of files and directories in its output. It leads to a recommendation to avoid parsing of the ls output. Others advise not using ls in variable declarations this way:

files=$(ls Documents/*.txt)

This declaration writes all TXT files of the Documents directory to the files variable. If there are spaces or line breaks in the filenames, we cannot separate them properly anymore.

Bash arrays solve the delimiter problem. An array stores a list of separate units. We can always read them in their original form. Therefore, we use an array to store the file names instead of a string. Here is a better declaration of the files variable:

declare -a files=(Documents/*.txt)

This command declares and initializes the array named files. Initializing means assigning values to the array’s elements.

When we declare a variable, Bash can deduce if it’s an array. This mechanism works when we skip the declare built-in. Bash adds the appropriate attribute automatically. Here is an example:

files=(Documents/*.txt)

This command declares the indexed array files.

Run the commands discussed in this lesson in the terminal below.

Get hands-on with 1400+ tech skills courses.