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