10.8. Arrays

10.8.1. What Is an Array?

In the TC shell, an array is simply a list of words, separated by spaces or tabs, and enclosed in parentheses. The elements of the array are numbered by subscripts starting at one. If there is not an array element for a subscript, the message "Subscript out of range" is displayed. Command substitution will also create an array. If the $# notation precedes an array name, the number of elements in the array is displayed.

Example 10.71.
1  > set fruit = ( apples pears peaches plums )
2  > echo $fruit
						apples pears peaches plums

3  > echo $fruit[1]
						Subscripts start at 1
						apples

4  > echo $fruit[2–4]
						Prints the 2nd, 3rd, and 4th elements
						pears peaches plums

5  > echo $fruit[6]
						Subscript out of range.

6  > echo ...

Get Linux Shells by Example 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.