summaryrefslogtreecommitdiffstats
path: root/tests/varenv.tests
blob: 68c619ec767f42e2d53b1fb4fc43dda46dffc5db (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#   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/>.
#
#
# varenv.sh
#
# Test the behavior of the shell with respect to variable and environment
# assignments
#
expect()
{
	echo expect "$@"
}

a=1
b=2
c=3
d=4
e=5
f=6 g=7 h=8

a=3 b=4 $CHMOD $MODE $FN

# This should echo "3 4" according to Posix.2
expect "3 4"
echo $a $b

set -k

# Assignment statements made when no words are left affect the shell's
# environment
a=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9

expect "5 6 7 8 9"
echo $a $b $c $d $e

$CHMOD f=7 $MODE g=8 $FN h=9
expect "7 8 9"
echo $f $g $h

set +k

# The temporary environment does not affect variable expansion, only the
# environment given to the command

export HOME=/usr/chet
expect $HOME
echo $HOME

expect $HOME
HOME=/a/b/c /bin/echo $HOME

expect $HOME
echo $HOME

# This should echo /a/b/c
expect /a/b/c
HOME=/a/b/c printenv HOME

set -k

# This should echo $HOME 9, NOT /a/b/c 9

expect "$HOME"
HOME=/a/b/c /bin/echo $HOME c=9
expect "$HOME 7"
echo $HOME $c

# I claim the next two echo calls should give identical output.
# ksh agrees, the System V.3 sh does not

expect "/a/b/c 9 /a/b/c"
HOME=/a/b/c $ECHO a=$HOME c=9
echo $HOME $c $a

expect "/a/b/c 9 /a/b/c"
HOME=/a/b/c a=$HOME c=9
echo $HOME $c $a
set +k

# How do assignment statements affect subsequent assignments on the same
# line?
expect "/a/b/c /a/b/c"
HOME=/a/b/c a=$HOME
echo $HOME $a

# The system V.3 sh does this wrong; the last echo should output "1 1",
# but the system V.3 sh has it output "2 2".  Posix.2 says the assignment
# statements are processed left-to-right.  bash and ksh output the right
# thing
c=1
d=2
expect "1 2"
echo $c $d
d=$c c=$d
expect "1 1"
echo $c $d

# just for completeness
unset d c
expect unset
echo ${d-unset}

# no output
export a
a=bcde
export a
/bin/true 2>/dev/null

func()
{
	local YYZ

	YYZ="song by rush"
	echo $YYZ
	echo $A
}

YYZ="toronto airport"
A="AVAR"
echo $YYZ
echo $A
A=BVAR func
echo $YYZ
echo $A

export A
# Make sure expansion doesn't use assignment statements preceding a builtin
A=ZVAR echo $A

XPATH=/bin:/usr/bin:/usr/local/bin:.
func2()
{
	local z=yy
	local -a avar=( ${XPATH//: } )
	echo ${avar[@]}
	local
}

avar=42
echo $avar
func2
echo $avar

# try to set an attribute for an unset variable; make sure it persists
# when the variable is assigned a value
declare -i ivar

ivar=10

declare -p ivar
unset ivar

# export an unset variable, make sure it is not suddenly set, but make
# sure the export attribute persists when the variable is assigned a
# value
export ivar
echo ${ivar-unset}

ivar=42
declare -p ivar

# make sure set [-+]o ignoreeof and $IGNOREEOF are reflected
unset IGNOREEOF
set +o ignoreeof
set -o ignoreeof
if [ "$IGNOREEOF" -ne 10 ]; then
	echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2
fi
unset IGNOREEOF
set +o ignoreeof

# older versions of bash used to not reset RANDOM in subshells correctly
[[ $RANDOM -eq $(echo $RANDOM) ]] && echo "RANDOM: problem with subshells"

# make sure that shopt -o is reflected in $SHELLOPTS
# first, get rid of things that might be set automatically via shell
# variables
set +o posix
set +o ignoreeof
set +o monitor
echo $-
echo ${SHELLOPTS}
shopt -so physical
echo $-
echo ${SHELLOPTS}

# and make sure it is readonly
readonly -p | grep SHELLOPTS

# This was an error in bash versions prior to bash-2.04.  The `set -a'
# should cause the assignment statement that's an argument to typeset
# to create an exported variable
unset FOOFOO
FOOFOO=bar
set -a
typeset FOOFOO=abcde

printenv FOOFOO

# test out export behavior of variable assignments preceding builtins and
# functions
$THIS_SH ./varenv1.sub

# more tests; bugs in bash up to version 2.05a
$THIS_SH ./varenv2.sub

# more tests; bugs in bash IFS scoping up through version 4.2
$THIS_SH ./varenv3.sub

# scoping problems with declare -g through bash-4.2
${THIS_SH} ./varenv4.sub

# more scoping and declaration problems with -g and arrays through bash-4.2
${THIS_SH} ./varenv5.sub

# variable scoping in the presence of nameref
${THIS_SH} ./varenv6.sub

# variable declaration problems with arrays and readonly local variables
${THIS_SH} ./varenv7.sub

# variable visibility problems with process substitution subshells in
# redirections
${THIS_SH} ./varenv8.sub

# make sure that builtins like readonly and export modify local array variables
# if executed in shell functions, like they modify local scalar variables
${THIS_SH} ./varenv9.sub

# more tests of unset and local variables with dynamic scoping
${THIS_SH} ./varenv10.sub

# tests of compound assignments in function scope
${THIS_SH} ./varenv11.sub

# temporary environment variable propagation and scoping in posix mode
${THIS_SH} ./varenv12.sub

# temporary environment and invalid shell identifier names
${THIS_SH} ./varenv13.sub

# localvar_inherit
${THIS_SH} ./varenv14.sub
${THIS_SH} ./varenv15.sub
${THIS_SH} ./varenv16.sub
${THIS_SH} ./varenv17.sub
${THIS_SH} ./varenv18.sub
${THIS_SH} ./varenv19.sub
${THIS_SH} ./varenv20.sub
${THIS_SH} ./varenv21.sub
${THIS_SH} ./varenv22.sub

# make sure variable scoping is done right
tt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a