#打印关联数组排序后的键
printSet() {
local -n items=$1
echo "$1 is ($(printf '%s\n' "${!items[@]}" | sort | paste -sd ' '))"
}
#bash 没有集合:关联数组的键是唯一的,
#所以可以用它们构成集合,值无关紧要
declare -A empty=()
#empty is ()
declare -A intSet=([1]=1 [2]=1 [3]=1)
#intSet is (1 2 3)
#重复添加会再次设置同一个键
declare -A strSet=()
for word in "one" "two" "three" "two"; do
strSet[$word]=1
done
#strSet is ("one" "three" "two")
#不加 declare -A 时是索引数组:键 "one" 在算术中被当作变量读取,
#其值为 0,所以两个单词都存入索引 0
notSet=()
notSet[one]=1
notSet[two]=1
notSetCount=${#notSet[@]}
#notSetCount is 1
printSet empty
printSet intSet
printSet strSet
echo "notSetCount is $notSetCount"