1 # TEST - basic declaration and assignment
15 declare fluff[qux]=assigned
21 # TEST - compount assignment and variable attributes
22 declare -A wheat chaff
23 wheat=( [zero]=0 [one]=a [two]=b [three]=c )
26 chaff=( [zero]=1+4 [one]=3+7 four )
28 declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
34 chaff[hello world]=flip
41 chaff=( [one]=a [*]=12 )
43 # TEST - key expansion -- no word splitting
44 chaff[hello world]=flip
46 echo ${chaff[hello world]}
48 chaff[box]="multiple words"
61 chaff=( [one]=a [*]=12 )
63 # TEST - keys and values containing spaces
66 wheat=([six]=6 [foo bar]="qux qix" )
71 declare -A wheat=([six]=6 [foo bar]="qux qix" )
73 recho ${wheat[foo bar]}
74 recho "${wheat[foo bar]}"
78 # TEST - basic expansions: number of elements and value length
81 wheat=([six]=6 [foo bar]="qux qix" )
85 recho ${#wheat[foo bar]}
87 # TEST - appending assignment operator
90 wheat=([six]=6 [foo bar]="qux qix" )
92 wheat[foo bar]+=' blat'
94 recho ${wheat[foo bar]}
95 recho "${wheat[foo bar]}"
100 wheat=([six]=6 [foo bar]=flix )
104 recho ${wheat[foo bar]}
105 recho "${wheat[foo bar]}"
108 # TEST - index expansion: no word splitting or globbing
112 wheat=([s*]=6 [foo bar]=flix )
118 # TEST -- associative array keys expansion
122 wheat=([six]=6 [foo bar]=flix )
127 # TEST -- associative array pattern removal
131 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
132 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
139 echo ${xpath[@]%%[!/]*}
140 echo ${xpath[0]%%[!/]*}
142 recho ${xpath%%[!/]*}
143 recho ${xpath[five]##*/}
144 recho ${xpath[five]%%[!/]*}
150 echo ${xpath[*]%%[!/]*}
152 # TEST -- associative array pattern substitution
156 xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
157 xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
160 # default element is "0" (as a string)
161 echo ${#xpath} -- ${xpath["0"]}
163 echo ${xpath[@]//\//^}
164 echo "${xpath[@]//\//^}" | cat -v
166 zecho "${xpath[@]/\//\\}"
167 zecho "${xpath[@]//\//\\}"
168 zecho "${xpath[@]//[\/]/\\}"
170 # test assignment to key "0"
177 # peculiar ksh93 semantics for unsubscripted assoc variable reference
180 if [ "$T" != "${T[0]}" ]; then
181 echo 'assoc.tests: $T and ${T[0]} mismatch'
184 ${THIS_SH} ./assoc1.sub
186 ${THIS_SH} ./assoc2.sub
188 ${THIS_SH} ./assoc3.sub
190 ${THIS_SH} ./assoc4.sub
192 ${THIS_SH} ./assoc5.sub
194 ${THIS_SH} ./assoc6.sub
196 ${THIS_SH} ./assoc7.sub
198 # test converting between scalars and assoc arrays
205 # weird syntax required to append to multiple existing array elements using
206 # compound assignment syntax
209 assoc=( [one]=one [two]=two [three]=three )
210 assoc+=( [one]+=more [two]+=less )
216 ${THIS_SH} ./assoc8.sub
218 # new shopt option to prevent multiple expansion of assoc array subscripts
219 ${THIS_SH} ./assoc9.sub
221 ${THIS_SH} ./assoc10.sub