]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/nameref17.sub
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / nameref17.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#
a0c0a00f
CR
14# test behavior of readonly namerefs and namerefs referencing readonly variables
15
16# readonly nameref variable referencing read-write global variable
17
18bar=one
19declare -rn foo0=bar
20unset foo0 # unsets bar
21declare -p bar
22unset -n foo0 # cannot unset
23declare -p foo0
24
25declare +r foo0 # modifies bar
26declare -p foo0 bar
27declare +r -n foo0 # error
28declare +n foo0 # error
29unset bar
30
31# readonly nameref variable without a value
32typeset -n foo1
33typeset -r foo1
34
35typeset -p foo1
36
37typeset foo1=bar # error
38typeset +r foo1 # no-op, follows nameref chain to nothing
39typeset -p foo1
40
41# nameref pointing to read-only global variable
42foo2=bar
43typeset -n foo2
44typeset -r foo2 # changes bar
45
46typeset -p foo2 bar
47
48foo2=bar # error?
49typeset +r foo2 # attempts to change bar, error
50typeset -p foo2 bar # nameref unchanged
51
52# read-only nameref pointing to read-only global variable
53bar3=three
54declare -rn foo3=bar3
55unset foo3 # unsets bar3
56
57bar3=three
58declare -p bar3
59unset -n foo3 # cannot unset
60
61readonly bar3
62declare +r foo3 # error attempting to reference bar3
63declare -p foo3 bar3
64declare +r -n foo3 # error
65
66# readonly nameref pointing to read-write local -- can we remove nameref attr?
67func()
68{
69 typeset bar4=four
70
71 # readonly nameref
72 typeset -n -r foo4=bar4
73
74 typeset -p foo4 bar4
75
76 typeset +n foo4
77
78 typeset -p foo4
79}
80func
81unset -f func
82
83# readonly nameref pointing to read-write global -- can we remove nameref attr?
84bar4=four
85foo4=bar4
86# readonly nameref
87typeset -n foo4
88typeset -r -n foo4
89
90typeset -p foo4 bar4
91
92typeset +n foo4
93typeset -p foo4
94
95bar4=four
96: ${foo4=bar4}
97
98typeset -p foo4 bar4
99
100# readonly local nameref without a value -- can we remove nameref attribute?
101func()
102{
103 declare -r -n foo5
104 declare -p foo5
105 declare +n foo5
106 declare -p foo5
107}
108func
109unset -f func
110
111# readonly global nameref without a value -- can we remove nameref attribute?
112declare -n foo5
113declare -r -n foo5
114declare -p foo5
115declare +n foo5
116declare -p foo5