Show Menu
Cheatography

bash array reference Cheat Sheet (DRAFT) by

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[­IND­EXN­R]=­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
${ARRA­Y[*]}
get all elements, split by IFS
${ARRA­Y[@]}
get all elements, split by space
${ARRA­Y[2]}
get third element
${#ARR­AY[­sub­scr­ipt]}
get length of ARRAY[­sub­script]
${#ARR­AY[@]}
get length of ARRAY
${ARRA­Y[@­]:s:n}
Retrieve n elements starting at index s