bash: Arrays

1 
1 6.7 Arrays
1 ==========
1 
1 Bash provides one-dimensional indexed and associative array variables.
1 Any variable may be used as an indexed array; the 'declare' builtin will
1 explicitly declare an array.  There is no maximum limit on the size of
1 an array, nor any requirement that members be indexed or assigned
1 contiguously.  Indexed arrays are referenced using integers (including
1 arithmetic expressions (⇒Shell Arithmetic)) and are zero-based;
1 associative arrays use arbitrary strings.  Unless otherwise noted,
1 indexed array indices must be non-negative integers.
1 
1    An indexed array is created automatically if any variable is assigned
1 to using the syntax
1      NAME[SUBSCRIPT]=VALUE
1 
1 The SUBSCRIPT is treated as an arithmetic expression that must evaluate
1 to a number.  To explicitly declare an array, use
1      declare -a NAME
1 The syntax
1      declare -a NAME[SUBSCRIPT]
1 is also accepted; the SUBSCRIPT is ignored.
1 
1 Associative arrays are created using
1      declare -A NAME.
1 
1    Attributes may be specified for an array variable using the 'declare'
1 and 'readonly' builtins.  Each attribute applies to all members of an
1 array.
1 
1    Arrays are assigned to using compound assignments of the form
1      NAME=(VALUE1 VALUE2 ... )
1 where each VALUE is of the form '[SUBSCRIPT]='STRING.  Indexed array
1 assignments do not require anything but STRING.  When assigning to
1 indexed arrays, if the optional subscript is supplied, that index is
1 assigned to; otherwise the index of the element assigned is the last
1 index assigned to by the statement plus one.  Indexing starts at zero.
1 
1    When assigning to an associative array, the subscript is required.
1 
1    This syntax is also accepted by the 'declare' builtin.  Individual
1 array elements may be assigned to using the 'NAME[SUBSCRIPT]=VALUE'
1 syntax introduced above.
1 
1    When assigning to an indexed array, if NAME is subscripted by a
1 negative number, that number is interpreted as relative to one greater
1 than the maximum index of NAME, so negative indices count back from the
1 end of the array, and an index of -1 references the last element.
1 
1    Any element of an array may be referenced using '${NAME[SUBSCRIPT]}'.
1 The braces are required to avoid conflicts with the shell's filename
1 expansion operators.  If the SUBSCRIPT is '@' or '*', the word expands
1 to all members of the array NAME.  These subscripts differ only when the
1 word appears within double quotes.  If the word is double-quoted,
1 '${NAME[*]}' expands to a single word with the value of each array
1 member separated by the first character of the 'IFS' variable, and
1 '${NAME[@]}' expands each element of NAME to a separate word.  When
1 there are no array members, '${NAME[@]}' expands to nothing.  If the
1 double-quoted expansion occurs within a word, the expansion of the first
1 parameter is joined with the beginning part of the original word, and
1 the expansion of the last parameter is joined with the last part of the
1 original word.  This is analogous to the expansion of the special
1 parameters '@' and '*'.  '${#NAME[SUBSCRIPT]}' expands to the length of
1 '${NAME[SUBSCRIPT]}'.  If SUBSCRIPT is '@' or '*', the expansion is the
1 number of elements in the array.  If the SUBSCRIPT used to reference an
1 element of an indexed array evaluates to a number less than zero, it is
1 interpreted as relative to one greater than the maximum index of the
1 array, so negative indices count back from the end of the array, and an
1 index of -1 refers to the last element.
1 
1    Referencing an array variable without a subscript is equivalent to
1 referencing with a subscript of 0.  Any reference to a variable using a
1 valid subscript is legal, and 'bash' will create an array if necessary.
1 
1    An array variable is considered set if a subscript has been assigned
1 a value.  The null string is a valid value.
1 
1    It is possible to obtain the keys (indices) of an array as well as
1 the values.  ${!NAME[@]} and ${!NAME[*]} expand to the indices assigned
1 in array variable NAME.  The treatment when in double quotes is similar
1 to the expansion of the special parameters '@' and '*' within double
1 quotes.
1 
1    The 'unset' builtin is used to destroy arrays.  'unset
1 NAME[SUBSCRIPT]' destroys the array element at index SUBSCRIPT.
1 Negative subscripts to indexed arrays are interpreted as described
1 above.  Care must be taken to avoid unwanted side effects caused by
1 filename expansion.  'unset NAME', where NAME is an array, removes the
1 entire array.  A subscript of '*' or '@' also removes the entire array.
1 
1    The 'declare', 'local', and 'readonly' builtins each accept a '-a'
1 option to specify an indexed array and a '-A' option to specify an
1 associative array.  If both options are supplied, '-A' takes precedence.
1 The 'read' builtin accepts a '-a' option to assign a list of words read
1 from the standard input to an array, and can read values from the
1 standard input into individual array elements.  The 'set' and 'declare'
1 builtins display array values in a way that allows them to be reused as
1 input.
1