]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/exp-tests
Imported from ../bash-2.0.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
235# Command substitution and the quirky differences between `` and $()
236
237expect '<\$x>'
238recho '\$x'
239
240expect '<$x>'
241recho `echo '\$x'`
242
243expect '<\$x>'
244recho $(echo '\$x')
245
246# The difference between $* "$*" and "$@"
247
248set "abc" "def ghi" "jkl"
249
250expect '<abc> <def> <ghi> <jkl>'
251recho $*
252
253expect '<abc def ghi jkl>'
254recho "$*"
255
256OIFS="$IFS"
257IFS=":$IFS"
258
259# The special behavior of "$*", using the first character of $IFS as separator
260expect '<abc:def ghi:jkl>'
261recho "$*"
262
263IFS="$OIFS"
264
265expect '<abc> <def ghi> <jkl>'
266recho "$@"
267
268expect '<xxabc> <def ghi> <jklyy>'
269recho "xx$@yy"
270
271expect '<abc> <def ghi> <jklabc> <def ghi> <jkl>'
272recho "$@$@"
273
274foo=abc
275bar=def
276
277expect '<abcdef>'
278recho "$foo""$bar"
279
280unset foo
281set $foo bar '' xyz "$foo" abc
282
283expect '<bar> <> <xyz> <> <abc>'
284recho "$@"
285
286# More tests of quoting and deferred evaluation
287
288foo=10 x=foo
289y='$'$x
290expect '<$foo>'
291recho $y
292eval y='$'$x
293expect '<10>'
294recho $y
295
296# case statements
297
298NL='
299'
300x='ab
301cd'
302
303expect '<newline expected>'
304case "$x" in
305*$NL*) recho "newline expected" ;;
306esac
307
308expect '<got it>'
309case \? in
310*"?"*) recho "got it" ;;
311esac
312
313expect '<got it>'
314case \? in
315*\?*) recho "got it" ;;
316esac
317
318set one two three four five
319expect '<one> <three> <five>'
320recho $1 $3 ${5} $8 ${9}
321expect '<5> <5>'
322recho $# ${#}
323
324expect '<42>'
325recho $((28 + 14))
326expect '<26>'
327recho $[ 13 * 2 ]
328
329expect '<\>'
330recho `echo \\\\`
331
332expect '<~>'
333recho '~'
334
335expect nothing
336recho $!
ccc6cda3
JA
337
338# test word splitting of assignment statements not preceding a command
339a="a b c d e"
340declare b=$a
341expect '<a> <b> <c> <d> <e>'
342recho $b