The gist
- Arrays are delimited by a space
Creating an array
local arr=(item1 item2 item3)
Creating an associative array
# Only Bash 4
declare -A myDict
myDict=(["key"]="value" ["key2"]="value2")
Looping over an array
local arr=(one two three)
# This loops over an assigned array
for item in "${arr[@]}"; do
echo "$item"
done
# This loops over an intermediate array
# Note that here, we delimit by comma instead of space
for item in {one, two, three}; do
echo "$item"
done
declare -A myDict
myDict=(["key"]="value" ["key2"]="value2")
# A loop over associative arrays (dicts).
# Note the ! when expanding the array
for key in "${!myDict[@]}"; do
echo "Key: $key"
echo "Value: ${myDict[$key]}"
done
Shifting array
arr=(item1 item2 item3)
echo "${arr[*]:1}"
> item2 item3