๐ Array
#optional
declare -a array
๐๏ธ Associative Array
#mandatory
declare -A assocArray
Important
Note the distinction between "-a" for array and "-A" for Associative arrays
๐ Array
array=(elem1 elem2 elem3)
๐๏ธ Associative Array
assocArray=([key1]=value1 [key2]=value2 [key3]=value3)
Tip
Don't hesitate to make use of "double quotes" if you need trickier values or keys
Note
The syntax is very similar to whole array affectation
๐ Array
array+=elemN
๐๏ธ Associative Array
assocArray+=([key1]=value1)
Note
You may have noticed but Pushing is basically a concatenation of a 1-sized array for both types
๐ Array
array+=(elemN, elemM, elemO)
๐๏ธ Associative Array
assocArray+=([key1]=value1 [key2]=value2 [key3]=value3)
๐ Array
array[0]=newElem1
Tip
You may set values for arbitrary indexes like just for array[13]
& array[12]
if you please
๐๏ธ Associative Array
assocArray[keyX]=valueX
๐ Array
echo ${array[0]}
๐๏ธ Associative Array
echo ${assocArray[keyX]}
๐ Array
echo "${array[@]}"
๐๏ธ Associative Array
echo "${assocArray[@]}"
๐ Array
echo "${array[*]}"
๐๏ธ Associative Array
echo "${assocArray[*]}"
echo "${!array[@]}"
๐๏ธ Associative Array
echo "${!assocArray[@]}"
Note
For either type of (Associative) Array the use of ${!array[@]}
is equivalent to ${!array[*]}
so have fun
๐ Array || ๐๏ธ Associative Array
echo ${#struct[@]}
Warning
Don't mistake ${#struct[@]}
with ${#struct[anIndex]}
which would be the size of the value at anIndex
in your struct
- Present this whole mess is a side by side array fashion
- (which would be way nicer than currently considering how ironic this is that it is about arrays but is not presented in a very visually structured manner)