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 ...