Saturday, October 30, 2010

Arrays in Bash (Cheatsheet)

Description Indexed array Associative array
Declare an array declare -a array declare -A array
Assignment array=(value1 value2 … valuen) array=([sub1]=value1 … [subN]=valueN)
Note:You must declare it first you can use
declare -A array=([sub1]=value1 … [subN]=valueN)
Expand to all values ${array[@]}  or ${array[*]} ${array[@]}  or ${array[*]}
Expand to all keys ${!array[@]}  or ${!array[*]} ${!array[@]}  or ${!array[*]}
length of an array ${#array[@]} or ${#array[*]} ${#array[@]} or ${#array[*]}
Remove entire array unset array or unset array[@] or unset array[*] unset array or unset array[@] or unset array[*]
Remove an element unset array[index] unset array[index]
Array join array1+=(${array2[@]}) or array1+=(v1 v2 … vN) array1+=([sub1]=v1 [sub2]=v2 … [subN]=vN)
Length of an element: ${#array[index]} ${#array[index]}

 

Resources

http://tldp.org/LDP/abs/html/arrays.html

No comments: