|
|
An array is created automatically if any variable is assigned to using the syntax name\fP[subscript\fP]=value\fP. The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. To explicitly declare an array, use declare -a name\fP (see .SM SHELL BUILTIN COMMANDS below). declare -a name\fP[subscript\fP] is also accepted; the subscript\fP is ignored. Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.
Arrays are assigned to using compound assignments of the form name\fP=(\fPvalue1\fP ... valuen\fP)\fP, where each value\fP is of the form [subscript\fP]=string\fP. Only string\fP is required. If the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero. This syntax is also accepted by the declare builtin. Individual array elements may be assigned to using the name\fP[subscript\fP]=value\fP syntax introduced above.
Any element of an array may be referenced using ${name\fP[subscript\fP]}. The braces are required to avoid conflicts with pathname expansion. If subscript\fP is @\fP or *\fP, the word expands to all members of name\fP. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name\fP[*]} expands to a single word with the value of each array member separated by the first character of the .SM IFS special variable, and ${name\fP[@]} expands each element of name\fP to a separate word. When there are no array members, ${name\fP[@]} expands to nothing. This is analogous to the expansion of the special parameters *\fP and @\fP (see Special Parameters above). ${#name\fP[subscript\fP]} expands to the length of ${name\fP[subscript\fP]}. If subscript\fP is *\fP or @\fP, the expansion is the number of elements in the array. Referencing an array variable without a subscript is equivalent to referencing element zero.
The unset builtin is used to destroy arrays. unset\fP name\fP[subscript\fP] destroys the array element at index subscript\fP. unset\fP name\fP, where name\fP is an array, or unset\fP name\fP[subscript\fP], where subscript\fP is *\fP or @\fP, removes the entire array.
The .BR declare , .BR local , and readonly builtins each accept a -a option to specify an array. The read builtin accepts a -a option to assign a list of words read from the standard input to an array. The set and declare builtins display array values in a way that allows them to be reused as assignments.
Web: http://shell.tjgy.com Email:support@tjgy.com