]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/varenv12.sub
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / varenv12.sub
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 #
14 set -o posix
15
16 fn() { foo=abc : ; typeset +x foo; printenv|grep ^foo=; }
17
18 fn
19 unset -v foo
20 unset -f fn
21
22 func1() {
23 var=1
24 var=2 : # or 'var=2 return', or another special builtin
25 unset -v var
26 echo $FUNCNAME: var = $var
27 }
28 func2() {
29 func1
30 unset -v var # bug: fails silently
31 }
32 func1
33 echo ${var+"BUG: still set 1"}
34
35 unset var
36 func2
37 echo ${var+"BUG: still set 2"}
38
39 unset -v var
40 unset -f func1 func2
41
42 fn() { foo=abc : ; typeset +x foo; echo -n 'inside: ' ; declare -p foo; }
43 fn
44 echo outside:
45 declare -p foo
46
47 unset -v foo
48 unset -f fn
49
50 func()
51 {
52 var=value declare -x var
53 echo -n 'inside: ' ; declare -p var
54 }
55
56 var=one
57 func
58 echo -n 'outside: ' ; declare -p var
59
60 unset -v var
61 unset -f func
62
63 # this will probably change behavior; export shouldn't behave like this when
64 # not in posix mode and the sequencing is probably wrong in posix mode. since
65 # export is a special builtin, the variable assignment should modify the
66 # local variable, as if a standalone assignment statement had been executed
67 # (posix modifying "the current execution environment") leaving the global
68 # variable unchanged. all shells, including bash, modify the local variable;
69 # bash was the only one that propagates the value out to the calling
70 # environment, but no longer does so.
71
72 func()
73 {
74 local var=inside
75 var=value export var
76 echo -n 'inside: ' ; declare -p var
77 }
78
79 var=outside
80 func
81 echo -n 'outside: ' ; declare -p var
82
83 unset -v var
84 unset -f func
85
86 func()
87 {
88 local var=local
89 var=inside :
90 echo -n 'inside: ' ; declare -p var
91 }
92
93 var=outside
94 func
95 echo -n 'outside: ' ; declare -p var
96
97 unset -v var
98 unset -f func
99
100 func()
101 {
102 echo -n 'inside func: ' ; echo "var=${var-<unset>}"
103 }
104
105 unset -v var
106 var=one :
107 echo -n 'outside 1.0: ' ; echo "var=${var-<unset>}"
108
109 unset -v var
110 var=one eval ':'
111 echo -n 'outside 1.1: ' ; echo "var=${var-<unset>}"
112
113 unset -v var
114
115 var=two func
116 echo -n 'outside 2.0: ' ; echo "var=${var-<unset>}"
117 var=global
118 var=two func
119 echo -n 'outside 2.1: ' ; echo "var=${var-<unset>}"
120
121 unset -v var
122 unset -f func
123
124 func1()
125 {
126 var=value export var
127 echo -n 'inside func1: ' ; echo "var=${var-<unset>}"
128 }
129
130 var=outside
131 func1
132 echo -n 'outside 3.0: ' ; echo "var=${var-<unset>}"
133
134 unset -v var
135 unset -f func1
136
137 func2()
138 {
139 local var=local
140 var=global :
141 echo -n 'inside func2: ' ; echo "var=${var-<unset>}"
142 }
143
144 var=outside
145 func2
146 echo -n 'outside 4.0: ' ; echo "var=${var-<unset>}"
147
148 unset -v var
149 unset -f fecho foo bar
150
151 fecho() {
152 echo $var
153 }
154
155 foo() {
156 local var="foo: bye bye"
157 var="foo: hello world" fecho
158 }
159
160 bar() {
161 var="bar: hello world" fecho
162 }
163
164 var=global
165 var=outside foo
166 echo after foo: var=$var
167 var=global
168 var=outside bar
169 echo after bar: var=$var
170
171 unset -v var