d45b702412fb7846da9e01895a193e9c50bfb200
[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 expect '<  abc> <def> <ghi> <jkl  >'
167 set abc def ghi jkl
168 recho "  $@  "
169
170 expect '<--abc> <def> <ghi> <jkl-->'
171 set abc def ghi jkl
172 recho "--$@--"
173
174 expect '<  >'
175 recho "  "
176 expect '< - >'
177 recho " - "
178
179 # Test combinations of different types of quoting in a fully-quoted string
180 # (so the WHOLLY_QUOTED tests fail and it doesn't get set)
181 expect '</^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/>'
182 recho "/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*"'$'"/\1/"
183
184 # Test the various Posix parameter expansions
185
186 expect '<foo bar>'
187 recho "${x:-$(echo "foo bar")}"
188 expect '<foo> <bar>'
189 recho ${x:-$(echo "foo bar")}
190
191 unset X
192 expect '<abc>'
193 recho ${X:=abc}
194 expect '<abc>'
195 recho $X
196
197 set a b c
198 expect '<posix>'
199 recho ${3:+posix}
200
201 POSIX=/usr/posix
202 expect '<10>'
203 recho ${#POSIX}
204
205 # remove shortest trailing match
206 x=file.c
207 expect '<file.o>'
208 recho ${x%.c}.o
209
210 # remove longest trailing match
211 x=posix/src/std
212 expect '<posix>'
213 recho ${x%%/*}
214
215 # remove shortest leading pattern
216 x=$HOME/src/cmd
217 expect '</src/cmd>'
218 recho ${x#$HOME}
219
220 # remove longest leading pattern
221 x=/one/two/three
222 expect '<three>'
223 recho ${x##*/}
224
225 # Command substitution and the quirky differences between `` and $()
226
227 expect '<\$x>'
228 recho '\$x'
229
230 expect '<$x>'
231 recho `echo '\$x'`
232
233 expect '<\$x>'
234 recho $(echo '\$x')
235
236 # The difference between $* "$*" and "$@"
237
238 set "abc" "def ghi" "jkl"
239
240 expect '<abc> <def> <ghi> <jkl>'
241 recho $*
242
243 expect '<abc def ghi jkl>'
244 recho "$*"
245
246 OIFS="$IFS"
247 IFS=":$IFS"
248
249 # The special behavior of "$*", using the first character of $IFS as separator
250 expect '<abc:def ghi:jkl>'
251 recho "$*"
252
253 IFS="$OIFS"
254
255 expect '<abc> <def ghi> <jkl>'
256 recho "$@"
257
258 expect '<xxabc> <def ghi> <jklyy>'
259 recho "xx$@yy"
260
261 expect '<abc> <def ghi> <jklabc> <def ghi> <jkl>'
262 recho "$@$@"
263
264 foo=abc
265 bar=def
266
267 expect '<abcdef>'
268 recho "$foo""$bar"
269
270 unset foo
271 set $foo bar '' xyz "$foo" abc
272
273 expect '<bar> <> <xyz> <> <abc>'
274 recho "$@"
275
276 # More tests of quoting and deferred evaluation
277
278 foo=10 x=foo
279 y='$'$x
280 expect '<$foo>'
281 recho $y
282 eval y='$'$x
283 expect '<10>'
284 recho $y
285
286 # case statements
287
288 NL='
289 '
290 x='ab
291 cd'
292
293 expect '<newline expected>'
294 case "$x" in
295 *$NL*)  recho "newline expected" ;;
296 esac
297
298 expect '<got it>'
299 case \? in
300 *"?"*) recho "got it" ;;
301 esac
302
303 expect '<got it>'
304 case \? in
305 *\?*) recho "got it" ;;
306 esac
307
308 set one two three four five
309 expect '<one> <three> <five>'
310 recho $1 $3 ${5} $8 ${9}
311 expect '<5> <5>'
312 recho $# ${#}
313
314 expect '<42>'
315 recho $((28 + 14))
316 expect '<26>'
317 recho $[ 13 * 2 ]
318
319 expect '<\>'
320 recho `echo \\\\`
321
322 expect '<~>'
323 recho '~'
324
325 expect nothing
326 recho $!