]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/varenv.tests
42fb54db52d541f9e340c33d2a8c05666090a7d1
[thirdparty/bash.git] / tests / varenv.tests
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 #
15 # varenv.sh
16 #
17 # Test the behavior of the shell with respect to variable and environment
18 # assignments
19 #
20 expect()
21 {
22 echo expect "$@"
23 }
24
25 a=1
26 b=2
27 c=3
28 d=4
29 e=5
30 f=6 g=7 h=8
31
32 a=3 b=4 $CHMOD $MODE $FN
33
34 # This should echo "3 4" according to Posix.2
35 expect "3 4"
36 echo $a $b
37
38 set -k
39
40 # Assignment statements made when no words are left affect the shell's
41 # environment
42 a=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9
43
44 expect "5 6 7 8 9"
45 echo $a $b $c $d $e
46
47 $CHMOD f=7 $MODE g=8 $FN h=9
48 expect "7 8 9"
49 echo $f $g $h
50
51 set +k
52
53 # The temporary environment does not affect variable expansion, only the
54 # environment given to the command
55
56 export HOME=/usr/chet
57 expect $HOME
58 echo $HOME
59
60 expect $HOME
61 HOME=/a/b/c /bin/echo $HOME
62
63 expect $HOME
64 echo $HOME
65
66 # This should echo /a/b/c
67 expect /a/b/c
68 HOME=/a/b/c printenv HOME
69
70 set -k
71
72 # This should echo $HOME 9, NOT /a/b/c 9
73
74 expect "$HOME"
75 HOME=/a/b/c /bin/echo $HOME c=9
76 expect "$HOME 7"
77 echo $HOME $c
78
79 # I claim the next two echo calls should give identical output.
80 # ksh agrees, the System V.3 sh does not
81
82 expect "/a/b/c 9 /a/b/c"
83 HOME=/a/b/c $ECHO a=$HOME c=9
84 echo $HOME $c $a
85
86 expect "/a/b/c 9 /a/b/c"
87 HOME=/a/b/c a=$HOME c=9
88 echo $HOME $c $a
89 set +k
90
91 # How do assignment statements affect subsequent assignments on the same
92 # line?
93 expect "/a/b/c /a/b/c"
94 HOME=/a/b/c a=$HOME
95 echo $HOME $a
96
97 # The system V.3 sh does this wrong; the last echo should output "1 1",
98 # but the system V.3 sh has it output "2 2". Posix.2 says the assignment
99 # statements are processed left-to-right. bash and ksh output the right
100 # thing
101 c=1
102 d=2
103 expect "1 2"
104 echo $c $d
105 d=$c c=$d
106 expect "1 1"
107 echo $c $d
108
109 # just for completeness
110 unset d c
111 expect unset
112 echo ${d-unset}
113
114 # no output
115 export a
116 a=bcde
117 export a
118 /bin/true 2>/dev/null
119
120 func()
121 {
122 local YYZ
123
124 YYZ="song by rush"
125 echo $YYZ
126 echo $A
127 }
128
129 YYZ="toronto airport"
130 A="AVAR"
131 echo $YYZ
132 echo $A
133 A=BVAR func
134 echo $YYZ
135 echo $A
136
137 export A
138 # Make sure expansion doesn't use assignment statements preceding a builtin
139 A=ZVAR echo $A
140
141 XPATH=/bin:/usr/bin:/usr/local/bin:.
142 func2()
143 {
144 local z=yy
145 local -a avar=( ${XPATH//: } )
146 echo ${avar[@]}
147 local
148 }
149
150 avar=42
151 echo $avar
152 func2
153 echo $avar
154
155 # try to set an attribute for an unset variable; make sure it persists
156 # when the variable is assigned a value
157 declare -i ivar
158
159 ivar=10
160
161 declare -p ivar
162 unset ivar
163
164 # export an unset variable, make sure it is not suddenly set, but make
165 # sure the export attribute persists when the variable is assigned a
166 # value
167 export ivar
168 echo ${ivar-unset}
169
170 ivar=42
171 declare -p ivar
172
173 # make sure set [-+]o ignoreeof and $IGNOREEOF are reflected
174 unset IGNOREEOF
175 set +o ignoreeof
176 set -o ignoreeof
177 if [ "$IGNOREEOF" -ne 10 ]; then
178 echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2
179 fi
180 unset IGNOREEOF
181 set +o ignoreeof
182
183 # older versions of bash used to not reset RANDOM in subshells correctly
184 [[ $RANDOM -eq $(echo $RANDOM) ]] && echo "RANDOM: problem with subshells"
185
186 # make sure that shopt -o is reflected in $SHELLOPTS
187 # first, get rid of things that might be set automatically via shell
188 # variables
189 set +o posix
190 set +o ignoreeof
191 set +o monitor
192 echo $-
193 echo ${SHELLOPTS}
194 shopt -so physical
195 echo $-
196 echo ${SHELLOPTS}
197
198 # and make sure it is readonly
199 readonly -p | grep SHELLOPTS
200
201 # This was an error in bash versions prior to bash-2.04. The `set -a'
202 # should cause the assignment statement that's an argument to typeset
203 # to create an exported variable
204 unset FOOFOO
205 FOOFOO=bar
206 set -a
207 typeset FOOFOO=abcde
208
209 printenv FOOFOO
210
211 # test out export behavior of variable assignments preceding builtins and
212 # functions
213 $THIS_SH ./varenv1.sub
214
215 # more tests; bugs in bash up to version 2.05a
216 $THIS_SH ./varenv2.sub
217
218 # more tests; bugs in bash IFS scoping up through version 4.2
219 $THIS_SH ./varenv3.sub
220
221 # scoping problems with declare -g through bash-4.2
222 ${THIS_SH} ./varenv4.sub
223
224 # more scoping and declaration problems with -g and arrays through bash-4.2
225 ${THIS_SH} ./varenv5.sub
226
227 # variable scoping in the presence of nameref
228 ${THIS_SH} ./varenv6.sub
229
230 # variable declaration problems with arrays and readonly local variables
231 ${THIS_SH} ./varenv7.sub
232
233 # variable visibility problems with process substitution subshells in
234 # redirections
235 ${THIS_SH} ./varenv8.sub
236
237 # make sure that builtins like readonly and export modify local array variables
238 # if executed in shell functions, like they modify local scalar variables
239 ${THIS_SH} ./varenv9.sub
240
241 # more tests of unset and local variables with dynamic scoping
242 ${THIS_SH} ./varenv10.sub
243
244 # tests of compound assignments in function scope
245 ${THIS_SH} ./varenv11.sub
246
247 # temporary environment variable propagation and scoping in posix mode
248 ${THIS_SH} ./varenv12.sub
249
250 # temporary environment and invalid shell identifier names
251 ${THIS_SH} ./varenv13.sub
252
253 # localvar_inherit
254 ${THIS_SH} ./varenv14.sub
255 ${THIS_SH} ./varenv15.sub
256 ${THIS_SH} ./varenv16.sub
257 ${THIS_SH} ./varenv17.sub
258 ${THIS_SH} ./varenv18.sub
259 ${THIS_SH} ./varenv19.sub
260 ${THIS_SH} ./varenv20.sub
261
262 # make sure variable scoping is done right
263 tt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a