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:
Individual array elements may be assigned to using the
name[subscript]=value
where:
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
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
With IFS being a space, you can transform a string to an array thanks to word splitting
string="a b c";
array=( $string )
echo ${array[0]} # a
echo ${array[1]} # b
# 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
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:
This syntax is also accepted by the declare builtin.
The set and declare builtins display array values in a way that allows them to be reused as assignments.
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 may be specified for an array variable using the:
Each attribute applies to all members of an array.
Any element of an array may be referenced using
${name[subscript]}
where:
Example with the builtin BASH_VERSINFO array.
echo ${BASH_VERSINFO[0]}
# same as:
echo ${BASH_VERSINFO}
4
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:
When there are no array members, Array[@] expands to nothing.
If the double-quoted expansion occurs within a word:
This is analogous to the expansion of the special parameters * and @.
${#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
Through the indices parameter expansion mechanism, it's possible to get a list of indices.
name[0]=value0
name[1]=value1
name[3]=value3
echo ${!name[*]}
0 1 3
declare -A colors
colors['yellow']=jaune
colors['red']=rouge
echo ${!colors[*]}
yellow red
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
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[@]
## 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
## declare an array variable
declare -a array=("element1" "element2" "element3")
##
for name in "${array[@]}"
do
printf "$name\n"
done
element1
element2
element3
(IFS=$'\n'; echo "${ARGS[*]}")
example
MY_ARRAY=("element1" "element2" "element3")
(IFS=$'\n'; echo "${MY_ARRAY[*]}")
element1
element2
element3
To check that an array contains a variable, you can:
declare -A myArray=( [value1]=1 [value2]=1 [value3]=1 )
x=value1
[[ -n "${array1[$x]}" ]]
[[ "${array1[*]}" =~ "value" ]] && echo contains || echo does not contains
man bash