This is a draft cheat sheet. It is a work in progress and is not finished yet.
Create / delete array
declare -a ARRAYNAME |
-a for array |
declare -A DICTNAME |
-A for dict |
ARRAY=(value1 value2 ... valueN) |
DICT=( [mark]=79 [john]=93 [ella]=87 [mila]=83 ) |
ARRAY[INDEXNR]=value |
directly set. no other value display |
read -a ARRAY |
read from user input |
unset ARRAY[1] |
delete only 1 element |
unset ARRAY |
delete whole array |
ARRAY+=(4) |
append |
ARRAY=( $(ls) ) |
put ls output into array |
|
|
get value
${!ARRAY[*]} |
index array of element |
${!DICT[*]} |
key array of elements |
${ARRAY[*]} |
get all elements, split by IFS |
${ARRAY[@]} |
get all elements, split by space |
${ARRAY[2]} |
get third element |
${#ARRAY[subscript]} |
get length of ARRAY[subscript] |
${#ARRAY[@]} |
get length of ARRAY |
${ARRAY[@]:s:n} |
Retrieve n elements starting at index s |
|