This draft deletes the entire topic.
Examples
-
List Assignment
Create an array with new elements:
array=('first element' 'second element' 'third element')
Subscript Assignment
Create an array with explicit element indices:
array=([3]='fourth element' [4]='fifth element')
Assignment by index
array[0]='first element' array[1]='second element'
Assignment by name (associative array)
declare -A array array[first]='First element' array[second]='Second element'
Dynamic Assignment
Create an array from the output of other command, for example use seq to get a range from 1 to 10:
array=(`seq 1 10`)
Assignment from script's input arguments:
array=("$@")
Assignment within loops:
while read -r; do #array+=("$REPLY") # Array append array[$i]="$REPLY" # Assignment by index let i++ # Increment index done < <(seq 1 10) # command substitution echo ${array[@]} # output: 1 2 3 4 5 6 7 8 9 10
where
$REPLY
is always the current input -
Print element at index 0
echo "${array[0]}"
Print last element (available from Bash 4.3)
echo "${array[-1]}"
Print all elements
echo "${array[@]}"
Print all elements as a single string
echo "${array[*]}"
Print all elements from index 1
echo "${array[@]:1}"
Print 3 elements from index 1
echo "${array[@]:1:3}"
String Operations
If referring to a single element, string-operations are permitted:
array=(zero one two) echo ${array[0]:0:3} # gives out zer (chars at position 0, 1 and 2 in the string zero) echo ${array[0]:1:3} # gives out ero (chars at position 1, 2 and 3 in the string zero)
so
${array[$i]:N:M}
gives out a string from theN
'th position (starting from 0) in the string${array[$i]}
withM
following chars -
-
Change Index
Initialize or update a particular element in the array
array[10]="elevenths element" # because it's starting with 0
Append
Modify array, adding elements to the end if no subscript is specified.
array+=('fourth element' 'fifth element')
Replace the entire array with a new parameter list.
array=("${array[@]}" "fourth element" "fifth element")
Add an element at the beginning:
array=("new element" "${array[@]}")
Insert
Insert an element at a given index:
arr=(a b c d) # insert an element at index 2 i=2 arr=("${arr[@]:0:$i}" 'new' "${arr[@]:$i}") echo "${arr[2]}" #output: new
Delete
Delete array indexes using the
unset
builtin:arr=(a b c) echo "${arr[@]}" # outputs: a b c echo "${!arr[@]}" # outputs: 0 1 2 unset -v 'arr[1]' echo "${arr[@]}" # outputs: a c echo "${!arr[@]}" # outputs: 0 2
Re-index an array
This can be useful if elements have been removed from an array, or if you're unsure whether there are gaps in the array. To recreates the indices:
array=("${array[@]}")
-
${#array[@]}
gives the length of the array${array[@]}
:array=('first element' 'second element' 'third element') echo "${#array[@]}" # gives out a length of 3
This works also with Strings in single elements:
echo "${#array[0]}" # gives out the lenght of the string at element 0: 13
-
Arrays can be iterated over easily:
fileList=( file1.txt file2.txt file3.txt ) # Within the for loop, $file is the current file for file in "${fileList[@]}" do echo "$file" done
-
To destroy, delete, or unset an array:
unset array
To destroy, delete, or unset a single array element:
unset array[10]
-
Our example array:
arr=(a b c d e f)
Using a
for..in
loop:for i in "${arr[@]}"; do echo "$i" done
Using
for
loop with numerical conditional:for ((i=0;i<${#arr[@]};i++)); do echo "${arr[$i]}" done
Using
while
loop:i=0 while [ $i -lt ${#arr[@]} ]; do echo "${arr[$i]}" i=$(expr $i + 1) done
Using
while
loop with numerical conditional:i=0 while (( $i < ${#arr[@]} ));do echo "${arr[$i]}" ((i++)) done
Using an
until
loop:i=0 until [ $i -ge ${#arr[@]} ];do echo "${arr[$i]}" i=$(expr $i + 1) done
Using an
until
loop with numerical conditional:i=0 until (( $i >= ${#arr[@]} )); do echo "${arr[$i]}" ((i++)) done
-
Get the list of inialized indexes in an array
$ arr[2]='second' $ arr[10]='tenth' $ arr[25]='twenty five' $ echo ${!arr[@]} 2 10 25
-
This function will insert an element into an array at a given index:
insert(){ h=' ################## insert ######################## # Usage: # insert arr_name index element # # Parameters: # arr_name : Name of the array variable # index : Index to insert at # element : Element to insert ################################################## ' [[ $1 = -h ]] && { echo "$h" >/dev/stderr; return 1; } declare -n __arr__=$1 # reference to the array variable i=$2 # index to insert at el="$3" # element to insert # handle errors [[ ! "$i" =~ ^[0-9]+$ ]] && { echo "E: insert: index must be a valid integer" >/dev/stderr; return 1; } (( $1 < 0 )) && { echo "E: insert: index can not be negative" >/dev/stderr; return 1; } # Now insert $el at $i __arr__=("${__arr__[@]:0:$i}" "$el" "${__arr__[@]:$i}") }
Usage:
insert array_variable_name index element
Example:
arr=(a b c d) echo "${arr[2]}" # output: c # Now call the insert function and pass the array variable name, # index to insert at # and the element to insert insert arr 2 'New Element' # 'New Element' was inserted at index 2 in arr, now print them echo "${arr[2]}" # output: New Element echo "${arr[3]}" # output: c
-
Associative arrays are supported by Bash 4.0+.
Declare an associative array
declare -A aa
Declaring an associative array before initialization or use is mandatory.
Initialize elements
You can initialize elements one at a time as follows:
aa[hello]=world aa[ab]=cd aa["key with space"]="hello world"
You can also initialize an entire associative array in a single statement:
aa=([hello]=world [ab]=cd ["key with space"]="hello world")
Access an Associative Array Element
echo ${aa[hello]} # Out: world
Listing Associative Array Keys
echo "${!aa[@]}" #Out: hello ab key with space
Listing Associative Array Values
echo "${aa[@]}" #Out: world cd hello world
Iterate Over Associative Array Keys and Values
for key in "${!aa[@]}"; do echo "Key: ${key}" echo "Value: ${array[$key]}" done # Out: # Key: hello # Value: world # Key: ab # Value: cd # Key: key with space # Value: hello world
Count Associative Array Elements
echo "${#aa[@]}" # Out: 3
-
stringVar="Apple Orange Banana Mango" arrayVar=(${stringVar// / })
Each space in the string denotes a new item in the resulting array.
echo ${arrayVar[0]} # will print Apple echo ${arrayVar[3]} # will print Mango
Similarly, other characters can be used for the delimiter.
stringVar="Apple+Orange+Banana+Mango" arrayVar=(${stringVar//+/ }) echo ${arrayVar[0]} # will print Apple echo ${arrayVar[2]} # will print Banana
Topic Outline
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft