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