Bash - Array Variable

1 - About

Bash provides one-dimensional array variables. See also: Bash - Flow statement (Control Structure)

When calling a function, quote the variable otherwise bash will not see the string as atomic. The separator is the variable $IFS.

There is:

  • no maximum limit on the size of an array,
  • nor any requirement that members be indexed or assigned contiguously.

3 - Management

3.1 - Assignment

3.1.1 - One value

3.1.1.1 - Implicit

Individual array elements may be assigned to using the

name[subscript]=value

where:

  • subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. Indexing: Arrays are indexed using integers and are zero-based.
  • name is the name of the array variable
  • value is the value of the variable for name[subscript]
3.1.1.2 - Explicit

The declare builtin will explicitly declare an array.

To explicitly declare an array, use

declare -a name
declare -a name[subscript] # is also accepted but the subscript is ignored
 
#Example
declare -a arr=("element1" "element2" "element3")

The following builtin command accept a -a option to specify an array

3.1.2 - Multiple value

Arrays are assigned to using compound assignments of the form

# Implicit
name=([subscript1]=string1... [subscriptN]=stringN)
array=("element1" "element2" "element3")
 
# Explicit
declare -a arr=("element1" "element2" "element3")

where:

  • Only string 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.

3.1.3 - Other

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.

3.2 - Operator (+=)

In the context where an assignment statement is assigning a value to a array index, the += operator, the variable’s value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array’s maximum index.

3.3 - Attributes

Attributes may be specified for an array variable using the:

Each attribute applies to all members of an array.

3.4 - Reference

3.4.1 - Index

Any element of an array may be referenced using

${name[subscript]}

where:

  • subscript is the index (default 0). Referencing an array variable without a subscript is equivalent to referencing element zero.
  • The braces are required to avoid conflicts with pathname expansion.

Example with the builtin BASH_VERSINFO array.

echo ${BASH_VERSINFO[0]}
# same as:
echo ${BASH_VERSINFO}
4

3.4.2 - Special subscript @ or *

If subscript is @ or *, the word expands to all members of 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 special variable
  • ${name[@]} expands each element of name to a separate word.

When there are no array members, ${name[@]} expands to nothing.

If the double-quoted expansion occurs within a word:

  • the expansion of the first parameter is joined with the beginning part of the original word
  • and the expansion of the last parameter is joined with the last part of the original word.

This is analogous to the expansion of the special parameters * and @.

3.5 - Number of element

${#name[subscript]} expands to the length of ${name[subscript]}.

This is a parameter expansion mechanism.

If subscript is * or @, the expansion is the number of elements in the array. Example with the builtin BASH_VERSINFO array.

echo ${#BASH_VERSINFO[*]}
6

3.6 - List of indices

Through the indices parameter expansion mechanism, it's possible to get a list of indices.

name[0]=value0
name[1]=value1
name[1]=value3
echo ${!name[*]}
  • Output
0 1 3

3.7 - Slicing

See parameter_expansion substring_expansion

Example with the BASH_VERSINFO, we can slice it to get the element 1 to 4

echo ${BASH_VERSINFO[@]:1:4}
3 46 1 release

3.8 - Unset (Destroy)

The unset builtin is used to destroy arrays.

To destroy the array element at index subscript.

unset name[subscript] 

Care must be taken to avoid unwanted side effects caused by filename generation.

The following statement removes the entire array.

unset name # where name is an array
# or 
unset name[*]
unset name[@]

3.9 - Looping

## declare an array variable
declare -a arr=("element1" "element2" "element3")
 
## 
for i in "${arr[@]}"
do
   echo "$i"
done

4 - Documentation / Reference

man bash
lang/bash/array.txt · Last modified: 2018/07/10 10:15 by gerardnico