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