]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/exp.tests
bash-5.1 beta release
[thirdparty/bash.git] / tests / exp.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 # A suite of tests for bash word expansions
16 #
17 # This tests parameter and variable expansion, with an empahsis on
18 # proper quoting behavior.
19 #
20 # Chet Ramey
21
22 #
23 # If you comment out the body of this function, you can do a diff against
24 # `expansion-tests.right' to see if the shell is behaving correctly
25 #
26 expect()
27 {
28 echo expect "$@"
29 }
30
31 # Test the substitution quoting characters (CTLESC and CTLNUL) in different
32 # combinations
33
34 expect "<^A>"
35 recho `echo '\ 1'`
36 expect "<^A>"
37 recho `echo "\ 1"`
38 expect "<^?>"
39 recho `echo '\7f'`
40 expect "<^?>"
41 recho `echo "\7f"`
42 expect "<^A>"
43 recho `echo \ 1`
44 expect "<^?>"
45 recho `echo \7f`
46
47 expect "bar"
48 recho ${foo:-"`echo bar`"}
49 expect "<^A>"
50 recho ${foo:-"`echo \ 1`"}
51 expect "<^?>"
52 recho ${foo:-"`echo \7f`"}
53
54 expect "<^A>"
55 recho "`echo \ 1`"
56 expect "<^?>"
57 recho "`echo \7f`"
58
59 # Test null strings without variable expansion
60 expect "<abcdefgh>"
61 recho abcd""efgh
62 expect "<abcdefgh>"
63 recho abcd''efgh
64 expect "<abcdefgh>"
65 recho ""abcdefgh
66 expect "<abcdefgh>"
67 recho ''abcdefgh
68 expect "<abcd>"
69 recho abcd""
70 expect "<abcd>"
71 recho abcd''
72
73 # Test the quirky behavior of $@ in ""
74 expect nothing
75 recho "$@"
76 expect "< >"
77 recho " $@"
78 expect "<-->"
79 recho "-${@}-"
80
81 # Test null strings with variable expansion that fails
82 expect '<>'
83 recho $xxx""
84 expect '<>'
85 recho ""$xxx
86 expect '<>'
87 recho $xxx''
88 expect '<>'
89 recho ''$xxx
90 expect '<>'
91 recho $xxx""$yyy
92 expect '<>'
93 recho $xxx''$yyy
94
95 # Test null strings with variable expansion that succeeds
96 xxx=abc
97 yyy=def
98
99 expect '<abc>'
100 recho $xxx""
101 expect '<abc>'
102 recho ""$xxx
103 expect '<abc>'
104 recho $xxx''
105 expect '<abc>'
106 recho ''$xxx
107 expect '<abcdef>'
108 recho $xxx""$yyy
109 expect '<abcdef>'
110 recho $xxx''$yyy
111
112 unset xxx yyy
113
114 # Test the unquoted special quoting characters
115 expect "<^A>"
116 recho \ 1
117 expect "<^?>"
118 recho \7f
119 expect "<^A>"
120 recho "\ 1"
121 expect "<^?>"
122 recho "\7f"
123 expect "<^A>"
124 recho '\ 1'
125 expect "<^?>"
126 recho '\7f'
127
128 # Test expansion of a variable that is unset
129 expect nothing
130 recho $xxx
131 expect '<>'
132 recho "$xxx"
133
134 expect nothing
135 recho "$xxx${@}"
136
137 # Test empty string expansion
138 expect '<>'
139 recho ""
140 expect '<>'
141 recho ''
142
143 # Test command substitution with (disabled) history substitution
144 expect '<Hello World!>'
145 # set +H
146 recho "`echo \"Hello world!\"`"
147
148 # Test some shell special characters
149 expect '<`>'
150 recho "\`"
151 expect '<">'
152 recho "\""
153 expect '<\^A>'
154 recho "\\ 1"
155
156 expect '<\$>'
157 recho "\\$"
158
159 expect '<\\>'
160 recho "\\\\"
161
162 # This should give argv[1] = a argv[2] = b
163 expect '<a> <b>'
164 FOO=`echo 'a b' | tr ' ' '\012'`
165 recho $FOO
166
167 # This should give argv[1] = ^A argv[2] = ^?
168 expect '<^A> <^?>'
169 FOO=`echo '\ 1 \7f' | tr ' ' '\012'`
170 recho $FOO
171
172 # Test quoted and unquoted globbing characters
173 expect '<**>'
174 recho "*"*
175
176 expect '<\.\./*/>'
177 recho "\.\./*/"
178
179 # Test patterns that come up when the shell quotes funny character
180 # combinations
181 expect '<^A^?^A^?>'
182 recho '\ 1\7f\ 1\7f'
183 expect '<^A^A>'
184 recho '\ 1\ 1'
185 expect '<^A^?>'
186 recho '\ 1\7f'
187 expect '<^A^A^?>'
188 recho '\ 1\ 1\7f'
189
190 # More tests of "$@"
191 set abc def ghi jkl
192 expect '< abc> <def> <ghi> <jkl >'
193 recho " $@ "
194 expect '< abc> <def> <ghi> <jkl >'
195 recho "${1+ $@ }"
196
197 set abc def ghi jkl
198 expect '<--abc> <def> <ghi> <jkl-->'
199 recho "--$@--"
200
201 set "a b" cd ef gh
202 expect '<a b> <cd> <ef> <gh>'
203 recho ${1+"$@"}
204 expect '<a b> <cd> <ef> <gh>'
205 recho ${foo:-"$@"}
206 expect '<a b> <cd> <ef> <gh>'
207 recho "${@}"
208
209 expect '< >'
210 recho " "
211 expect '< - >'
212 recho " - "
213
214 # Test combinations of different types of quoting in a fully-quoted string
215 # (so the WHOLLY_QUOTED tests fail and it doesn't get set)
216 expect '</^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/>'
217 recho "/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*"'$'"/\1/"
218
219 # Test the various Posix parameter expansions
220
221 expect '<foo bar>'
222 recho "${x:-$(echo "foo bar")}"
223 expect '<foo> <bar>'
224 recho ${x:-$(echo "foo bar")}
225
226 unset X
227 expect '<abc>'
228 recho ${X:=abc}
229 expect '<abc>'
230 recho $X
231
232 set a b c
233 expect '<posix>'
234 recho ${3:+posix}
235
236 POSIX=/usr/posix
237 expect '<10>'
238 recho ${#POSIX}
239
240 # remove shortest trailing match
241 x=file.c
242 expect '<file.o>'
243 recho ${x%.c}.o
244
245 # remove longest trailing match
246 x=posix/src/std
247 expect '<posix>'
248 recho ${x%%/*}
249
250 # remove shortest leading pattern
251 x=$HOME/src/cmd
252 expect '</src/cmd>'
253 recho ${x#$HOME}
254
255 # remove longest leading pattern
256 x=/one/two/three
257 expect '<three>'
258 recho ${x##*/}
259
260 # pattern removal of patterns that don't match
261 z=abcdef
262
263 expect '<abcdef>'
264 recho ${z#xyz}
265 expect '<abcdef>'
266 recho ${z##xyz}
267
268 expect '<abcdef>'
269 recho ${z%xyz}
270 expect '<abcdef>'
271 recho ${z%%xyz}
272
273 # Command substitution and the quirky differences between `` and $()
274
275 expect '<\$x>'
276 recho '\$x'
277
278 expect '<$x>'
279 recho `echo '\$x'`
280
281 expect '<\$x>'
282 recho $(echo '\$x')
283
284 # The difference between $* "$*" and "$@"
285
286 set "abc" "def ghi" "jkl"
287
288 expect '<abc> <def> <ghi> <jkl>'
289 recho $*
290
291 expect '<abc def ghi jkl>'
292 recho "$*"
293
294 OIFS="$IFS"
295 IFS=":$IFS"
296
297 # The special behavior of "$*", using the first character of $IFS as separator
298 expect '<abc:def ghi:jkl>'
299 recho "$*"
300
301 IFS="$OIFS"
302
303 expect '<abc> <def ghi> <jkl>'
304 recho "$@"
305
306 expect '<xxabc> <def ghi> <jklyy>'
307 recho "xx$@yy"
308
309 expect '<abc> <def ghi> <jklabc> <def ghi> <jkl>'
310 recho "$@$@"
311
312 foo=abc
313 bar=def
314
315 expect '<abcdef>'
316 recho "$foo""$bar"
317
318 unset foo
319 set $foo bar '' xyz "$foo" abc
320
321 expect '<bar> <> <xyz> <> <abc>'
322 recho "$@"
323
324 # More tests of quoting and deferred evaluation
325
326 foo=10 x=foo
327 y='$'$x
328 expect '<$foo>'
329 recho $y
330 eval y='$'$x
331 expect '<10>'
332 recho $y
333
334 # case statements
335
336 NL='
337 '
338 x='ab
339 cd'
340
341 expect '<newline expected>'
342 case "$x" in
343 *$NL*) recho "newline expected" ;;
344 esac
345
346 expect '<got it>'
347 case \? in
348 *"?"*) recho "got it" ;;
349 esac
350
351 expect '<got it>'
352 case \? in
353 *\?*) recho "got it" ;;
354 esac
355
356 set one two three four five
357 expect '<one> <three> <five>'
358 recho $1 $3 ${5} $8 ${9}
359
360 # length tests on positional parameters and some special parameters
361
362 expect '<5> <5>'
363 recho $# ${#}
364 expect '<3>'
365 recho ${#1}
366 expect '<1>'
367 recho ${##}
368 expect '<1>'
369 recho ${#?}
370 expect '<5>'
371 recho ${#@}
372 expect '<5>'
373 recho ${#*}
374 expect '<5>'
375 recho "${#@}"
376 expect '<5>'
377 recho "${#*}"
378
379 expect '<42>'
380 recho $((28 + 14))
381 expect '<26>'
382 recho $[ 13 * 2 ]
383
384 expect '<\>'
385 recho `echo \\\\`
386
387 expect '<~>'
388 recho '~'
389
390 expect nothing
391 recho $!
392 expect nothing
393 recho ${!}
394
395 # test word splitting of assignment statements not preceding a command
396 a="a b c d e"
397 declare b=$a
398 expect '<a> <b> <c> <d> <e>'
399 recho $b
400
401 a="a?b?c"
402
403 echo ${a//\\?/ }
404
405 echo ${a//\?/ }
406
407 ${THIS_SH} -c 'var=a:b: ; IFS=" :" ; recho $var""' bash
408
409 ${THIS_SH} ./exp1.sub
410
411 ${THIS_SH} ./exp2.sub
412
413 ${THIS_SH} ./exp3.sub
414
415 ${THIS_SH} ./exp4.sub
416
417 ${THIS_SH} ./exp5.sub
418
419 ${THIS_SH} ./exp6.sub
420 ${THIS_SH} ./exp7.sub
421 ${THIS_SH} ./exp8.sub
422 ${THIS_SH} ./exp9.sub
423 ${THIS_SH} ./exp10.sub
424 ${THIS_SH} ./exp11.sub
425 ${THIS_SH} ./exp12.sub