]>
Commit | Line | Data |
---|---|---|
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 | # | |
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 "<^B>" | |
26 | recho `echo '\ 2'` | |
27 | expect "<^B>" | |
28 | recho `echo "\ 2"` | |
29 | expect "<^A>" | |
30 | recho `echo \ 1` | |
31 | expect "<^B>" | |
32 | recho `echo \ 2` | |
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 "<^B>" | |
93 | recho \ 2 | |
94 | expect "<^A>" | |
95 | recho "\ 1" | |
96 | expect "<^B>" | |
97 | recho "\ 2" | |
98 | expect "<^A>" | |
99 | recho '\ 1' | |
100 | expect "<^B>" | |
101 | recho '\ 2' | |
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] = ^B | |
143 | expect '<^A> <^B>' | |
144 | FOO=`echo '\ 1 \ 2' | 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^B^A^B>' | |
157 | recho '\ 1\ 2\ 1\ 2' | |
158 | expect '<^A^A>' | |
159 | recho '\ 1\ 1' | |
160 | expect '<^A^B>' | |
161 | recho '\ 1\ 2' | |
162 | expect '<^A^A^B>' | |
163 | recho '\ 1\ 1\ 2' | |
164 | ||
165 | # More tests of "$@" | |
726f6388 | 166 | set abc def ghi jkl |
ccc6cda3 | 167 | expect '< abc> <def> <ghi> <jkl >' |
726f6388 | 168 | recho " $@ " |
ccc6cda3 JA |
169 | expect '< abc> <def> <ghi> <jkl >' |
170 | recho "${1+ $@ }" | |
726f6388 | 171 | |
726f6388 | 172 | set abc def ghi jkl |
ccc6cda3 | 173 | expect '<--abc> <def> <ghi> <jkl-->' |
726f6388 JA |
174 | recho "--$@--" |
175 | ||
ccc6cda3 JA |
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 | ||
726f6388 JA |
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 | ||
d166f048 JA |
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 | ||
726f6388 JA |
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} | |
d166f048 JA |
334 | |
335 | # length tests on positional parameters and some special parameters | |
336 | ||
726f6388 JA |
337 | expect '<5> <5>' |
338 | recho $# ${#} | |
d166f048 JA |
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 "${#*}" | |
726f6388 JA |
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 $! | |
d166f048 JA |
367 | expect nothing |
368 | recho ${!} | |
ccc6cda3 JA |
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 | |
95732b49 JA |
375 | |
376 | a="a?b?c" | |
377 | ||
378 | echo ${a//\\?/ } | |
379 | ||
380 | echo ${a//\?/ } | |
3185942a JA |
381 | |
382 | ${THIS_SH} ./exp1.sub | |
383 | ||
384 | ${THIS_SH} ./exp2.sub | |
0001803f CR |
385 | |
386 | ${THIS_SH} ./exp3.sub | |
495aee44 CR |
387 | |
388 | ${THIS_SH} ./exp4.sub |