GNU Info

Info Node: (bashref.info)Arrays

(bashref.info)Arrays


Next: The Directory Stack Prev: Aliases Up: Bash Features
Enter node , (file) or (file)node

Arrays
======

   Bash provides one-dimensional array variables.  Any variable may be
used as an array; the `declare' builtin will explicitly declare an
array.  There is no maximum limit on the size of an array, nor any
requirement that members be indexed or assigned contiguously.  Arrays
are zero-based.

   An array is created automatically if any variable is assigned to
using the syntax
     name[SUBSCRIPT]=VALUE

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

The syntax
     declare -a NAME[SUBSCRIPT]

is also accepted; the SUBSCRIPT 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=(value1 ... valueN)

where each VALUE is of the form `[[SUBSCRIPT]=]'STRING.  If the
optional subscript is 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['SUBSCRIPT`]='VALUE syntax introduced above.

   Any element of an array may be referenced using
`${name['SUBSCRIPT`]}'.  The braces are required to avoid conflicts
with the shell's filename expansion operators.  If the SUBSCRIPT is `@'
or `*', the word expands to all members of the array NAME.  These
subscripts differ only when the word appears within double quotes.  If
the word is double-quoted, `${name[*]}' expands to a single word with
the value of each array member separated by the first character of the
`IFS' variable, and `${name[@]}' expands each element of NAME to a
separate word.  When there are no array members, `${name[@]}' expands
to nothing.  This is analogous to the expansion of the special
parameters `@' and `*'.  `${#name['SUBSCRIPT`]}' expands to the length
of `${name['SUBSCRIPT`]}'.  If SUBSCRIPT is `@' or `*', 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'
NAME[SUBSCRIPT] destroys the array element at index SUBSCRIPT.  `unset'
NAME, where NAME is an array, removes the entire array. A subscript of
`*' or `@' also removes the entire array.

   The `declare', `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, and
can read values from the standard input into individual array elements.
The `set' and `declare' builtins display array values in a way that
allows them to be reused as input.


automatically generated by info2www version 1.2.2.9