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