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