]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/varenv.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / varenv.tests
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
14#
15# varenv.sh
16#
17# Test the behavior of the shell with respect to variable and environment
18# assignments
19#
20expect()
21{
22 echo expect "$@"
23}
24
25a=1
26b=2
27c=3
28d=4
29e=5
30f=6 g=7 h=8
31
32a=3 b=4 $CHMOD $MODE $FN
33
34# This should echo "3 4" according to Posix.2
35expect "3 4"
36echo $a $b
37
38set -k
39
40# Assignment statements made when no words are left affect the shell's
41# environment
42a=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9
43
44expect "5 6 7 8 9"
45echo $a $b $c $d $e
46
47$CHMOD f=7 $MODE g=8 $FN h=9
48expect "7 8 9"
49echo $f $g $h
50
51set +k
52
53# The temporary environment does not affect variable expansion, only the
54# environment given to the command
55
56export HOME=/usr/chet
57expect $HOME
58echo $HOME
59
60expect $HOME
61HOME=/a/b/c /bin/echo $HOME
62
63expect $HOME
64echo $HOME
65
66# This should echo /a/b/c
67expect /a/b/c
68HOME=/a/b/c printenv HOME
69
70set -k
71
72# This should echo $HOME 9, NOT /a/b/c 9
73
74expect "$HOME"
75HOME=/a/b/c /bin/echo $HOME c=9
76expect "$HOME 7"
77echo $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
82expect "/a/b/c 9 /a/b/c"
83HOME=/a/b/c $ECHO a=$HOME c=9
84echo $HOME $c $a
85
86expect "/a/b/c 9 /a/b/c"
87HOME=/a/b/c a=$HOME c=9
88echo $HOME $c $a
89set +k
90
91# How do assignment statements affect subsequent assignments on the same
92# line?
93expect "/a/b/c /a/b/c"
94HOME=/a/b/c a=$HOME
95echo $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
101c=1
102d=2
103expect "1 2"
104echo $c $d
105d=$c c=$d
106expect "1 1"
107echo $c $d
108
109# just for completeness
110unset d c
111expect unset
112echo ${d-unset}
113
114# no output
115export a
116a=bcde
117export a
118/bin/true 2>/dev/null
119
120func()
121{
122 local YYZ
123
124 YYZ="song by rush"
125 echo $YYZ
126 echo $A
127}
128
129YYZ="toronto airport"
130A="AVAR"
131echo $YYZ
132echo $A
133A=BVAR func
134echo $YYZ
135echo $A
136
137export A
138# Make sure expansion doesn't use assignment statements preceding a builtin
139A=ZVAR echo $A
140
141XPATH=/bin:/usr/bin:/usr/local/bin:.
142func2()
143{
144 local z=yy
145 local -a avar=( ${XPATH//: } )
146 echo ${avar[@]}
147 local
148}
149
150avar=42
151echo $avar
152func2
153echo $avar
154
155# try to set an attribute for an unset variable; make sure it persists
156# when the variable is assigned a value
157declare -i ivar
158
159ivar=10
160
161declare -p ivar
162unset 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
167export ivar
168echo ${ivar-unset}
169
170ivar=42
171declare -p ivar
172
173# make sure set [-+]o ignoreeof and $IGNOREEOF are reflected
174unset IGNOREEOF
175set +o ignoreeof
176set -o ignoreeof
177if [ "$IGNOREEOF" -ne 10 ]; then
178 echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2
179fi
180unset IGNOREEOF
181set +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
189set +o posix
190set +o ignoreeof
191set +o monitor
192echo $-
193echo ${SHELLOPTS}
194shopt -so physical
195echo $-
196echo ${SHELLOPTS}
197
198# and make sure it is readonly
199readonly -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
204unset FOOFOO
205FOOFOO=bar
206set -a
207typeset FOOFOO=abcde
208
209printenv 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
8868edaf 250# temporary environment and invalid shell identifier names
d233b485
CR
251${THIS_SH} ./varenv13.sub
252
253# localvar_inherit
254${THIS_SH} ./varenv14.sub
d233b485 255${THIS_SH} ./varenv15.sub
8868edaf
CR
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${THIS_SH} ./varenv21.sub
d233b485
CR
262
263# make sure variable scoping is done right
264tt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a