]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/assoc9.sub
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / tests / assoc9.sub
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
d233b485
CR
14typeset -A a=( [\\]= [\"]= [\)]= ) b
15for x in "${!a[@]}"; do b[$x]=; done
16b+=([\`]= [\]]=)
17typeset -p b
18for x in "${!a[@]}"; do
19 unset -v 'b[$x]'
20done
21typeset -p b
22
23unset -v a b
24
25loaddict()
26{
27 dict['"']=1
28 dict['`']=2
29 dict["'"]=3
30 dict['\']=4
31 declare -p dict
32}
33
34del()
35{
36 unset -v dict["$1"];
37}
38
39declare -A dict
40loaddict
41for k in "${!dict[@]}"; do del "$k"; done
42declare -p dict
43
44unset 'dict[@]'
45
46shopt -s assoc_expand_once
47declare -A dict
48loaddict
49for k in "${!dict[@]}"; do del "$k"; done
50declare -p dict
51
52unset a b dict
53
54typeset -A a
55b="80's"
56
57((++a[$b]))
58
59((++a["$b"]))
60[[ $((++a[$b])) ]]
61[[ $((++a["$b"])) ]]
62echo ${a["$b"]}
63echo ${a[$b]}
64
65let "++a[$b]"
66
67echo 'a[$b]=' "${a[$b]}"
68
69unset a b
70
71declare -A a
72b="80's"
73
74: ${a[$b]:='Depeche Mode'}
75
76declare -p a
77
78unset a b
79shopt -u assoc_expand_once
80
81typeset -A a
82b="80's"
83
84read a[$b] <<<"Depeche Mode"
85typeset -p a
86
87shopt -s assoc_expand_once
88read a[$b] <<<"Depeche Mode"
89typeset -p a
90
91unset a
92shopt -u assoc_expand_once
93
94typeset -A a
95
96printf -v a[$b] "%s" "Depeche Mode"
97typeset -p a
98
99shopt -s assoc_expand_once
100
101printf -v a[$b] "%s" "Depeche Mode"
102typeset -p a
103
104unset a
105
106declare -A a
107x='$(date >&2)'
108a[$x]=5
109
110shopt -s assoc_expand_once
111echo $((1+a[$x]))
112echo $((1+a[\$x]))
113echo "1+${a[$x]}"
114
115declare -p a
116
117shopt -s assoc_expand_once
118declare -A myarray
119
120declare myarray["foo[bar"]=bleh
121myarray["foo"]=bleh
122
123declare -p myarray
8868edaf
CR
124
125key='$(date >&2)'
126declare -A aa
127aa[$key]=foo
128echo "${aa[$key]}"
129
130[[ -v aa[$key] ]] || echo bad assoc expansion
74091dd4
CR
131
132shopt -u assoc_expand_once
133
134declare -A assoc
135
136var=x123
137assoc['$var']=value
138
139declare -p assoc
140
141unset "assoc[$var]"
142declare -p assoc
143
144unset 'assoc[$var]'
145declare -p assoc
146
147shopt -s assoc_expand_once
148unset 'assoc[$var]'
149declare -p assoc
150
151typeset -A foo
152
153foo["foo]bar"]=bip
154typeset foo["foo]bar"]=bax
155
156echo ${!foo[@]}
157echo ${foo[@]}
158
159declare -p foo