About
Bash provides a one-dimensional array variables. See also: Bash Shell and (Unix|Linux) Utilities (XCU)
When calling a function, quote the variable otherwise bash will not see the string as atomic. The separator is variable $IFS.
There is:
- no maximum limit on the size of an array,
- nor any requirement that members be indexed or assigned contiguously.
Management
Assignment
One value
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.
- value is the value of the variable for name[subscript]
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
- and readonly .
Associative
By default, if not defined, the array is indexed by integer. If you want to have an associative array, you need to declare it
declare -A arr
declare -A name=(key1 value1 key2 value2 … )
declare -A name=( [key1]=value1 [key2]=value2 … )
Multiple values - string word splitting
With IFS being a space, you can transform a string to an array thanks to word splitting
- Explicit:
string="a b c";
array=( $string )
echo ${array[0]} # a
echo ${array[1]} # b
- Implicit: The read builtin accepts a -a option to assign a list of words read from the standard input to an array.
# string
IFS=', ' read -r -a array <<< "element1, element2, element3"
# file (every value on one line)
IFS=$'\n' read -r -a array -d $'\c0' < /path/to/file
Multiple values
Arrays are assigned using compound assignments of the form
# Implicit
name=([subscript1]=string1... [subscriptN]=stringN)
array=("element1" "element2" "element3")
# Explicit
declare -a array=("element1" "element2" "element3")
IFS=', ' read -r -a array <<< "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.
Show the array definition with typeset
typeset -p myarray
declare -A myarray=([author]="jon" )
Other
The set and declare builtins display array values in a way that allows them to be reused as assignments.
Append
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.
Example
declare -a myArray
myArray+=(first)
myArray+=(second)
Attributes
Attributes may be specified for an array variable using the:
- and readonly builtins.
Each attribute applies to all members of an array.
Reference
Index
Any element of an array may be referenced using
${name[subscript]}
where:
- subscript is the index (default 0, may be negative). 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
Last element
declare -a args=("element1" "element2" "element3")
echo ${args[-1]}
element3
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:
- Array[*] expands to a single word with the value of each array member separated by the first character of the IFS special variable
- Array[@] expands each element of name to a separate word.
When there are no array members, Array[@] 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 @.
Number of elements
${#name[subscript]} expands to the length of ${name[subscript]}.
This is a parameter expansion mechanism.
If the subscript is * or @, the expansion is the number of elements in the array. Example with the builtin BASH_VERSINFO array.
echo ${#BASH_VERSINFO[*]}
6
List of indices
Through the indices parameter expansion mechanism, it's possible to get a list of indices.
- Default
name[0]=value0
name[1]=value1
name[3]=value3
echo ${!name[*]}
- Output
0 1 3
declare -A colors
colors['yellow']=jaune
colors['red']=rouge
echo ${!colors[*]}
yellow red
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
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[@]
Looping
- index looping with Bash - For Statement
## declare an array variable
IFS=' ' read -r -a array <<< "element1 element2 element3"
## Loop
for i in "${!array[@]}"
do
printf "$i - ${array[$i]}\n"
done
0 - element1
1 - element2
2 - element3
- name looping
## declare an array variable
declare -a array=("element1" "element2" "element3")
##
for name in "${array[@]}"
do
printf "$name\n"
done
element1
element2
element3
Print / Explode
- Set the IFS separator and print in a subshell to not set IFS in the current session
(IFS=$'\n'; echo "${ARGS[*]}")
example
MY_ARRAY=("element1" "element2" "element3")
(IFS=$'\n'; echo "${MY_ARRAY[*]}")
element1
element2
element3
Contains
To check that an array contains a variable, you can:
- declare the value as key and check
declare -A myArray=( [value1]=1 [value2]=1 [value3]=1 )
x=value1
[[ -n "${array1[$x]}" ]]
- a regular expression on the concatenation
[[ "${array1[*]}" =~ "value" ]] && echo contains || echo does not contains
Documentation / Reference
man bash