summaryrefslogtreecommitdiffstats
path: root/tests/quotearray5.sub
blob: 5366a9997700f7d7fafed301b648c94339dafe0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# a set of tests for unset to try to ensure that subscripts are only expanded
# once. Derived from tests submitted by konsolebox@gmail.com

declare -A a
key='$(echo foo)'

# Here the tokens are valid array references and pass that fact to unset
# post-expansion

# This solves the surprise expansion issues.

a[$key]=1
unset -v a[$key]		# this performs normal word splitting
unset -v a["$key"]		# prevent word splitting
declare -p a # Displays no element

a['$key']=2
unset -v a['$key']
declare -p a # Displays no element

a["foo"]=3
unset -v a["foo"]
declare -p a # Displays no element

echo -----

# Here the tokens are "strings".  They expand and keep the
# original behavior and allows existing scripts to not break.
# It also allows nref or iref references to be transparently
# referenced in it.

# the quotes prevent the arguments from being recognized as valid array
# references before word expansion. since unset doesn't know to treat
# them specially, they're treated as in previous versions and expansion
# is performed as part of evaluating the subscript

a[$key]=1
declare -p a
unset 'a[$key]' # Transforms to a[$key] after expansion
declare -p a # Displays no element

a['$key']=2
unset "a['\$key']" # Transforms to a['$key'] after expansion
declare -p a # Displays no element

a["foo"]=3
unset 'a["foo"]' # Transforms to a["foo"] after expansion
declare -p a # Displays no element

echo -----

# The update also keeps compatibility with already existing behavior of
# unset when assoc_expand_once is enabled, but only for quoted tokens.

a=()
shopt -s assoc_expand_once

a[$key]=1
unset "a[$key]"
declare -p a # Displays no element

a['$key']=2
unset "a[\$key]"
declare -p a # Displays no element

a["foo"]=3
unset "a[foo]"
declare -p a # Displays no element

echo ----------

# For unsetting '@' and all elements:

key=@

declare -A a=(@ v0 . v1)
unset a[$key]
declare -p a # Displays 'declare -A a=([.]="v1" )'

declare -A a=(@ v0 . v1)
unset a[@]
declare -p a # same behavior

echo -----

# these are quoted strings and unset doesn't treat them specially

unset a
shopt -u assoc_expand_once

declare -A a=(@ v0 . v1)
unset 'a[$key]'
declare -p a # Displays 'declare -A a=([.]="v1" )'

declare -A a=(@ v0 . v1)
unset 'a[@]'
declare -p a # same behavior

echo -----

unset a
shopt -s assoc_expand_once

declare -A a=(@ v0 . v1)
unset "a[$key]"			# $key is expanded
declare -p a # Displays 'declare -A a=([.]="v1" )'

declare -A a=(@ v0 . v1)
unset 'a[@]'
declare -p a # same behavior