]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/more-exp.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / more-exp.tests
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14 expect()
15 {
16 echo expect "$@"
17 }
18
19 tool_var() {
20 eval $1=\"\${$1:-$2}\"
21 export $1
22 }
23
24 A="aaa bbb ccc"
25
26 unset B
27
28 tool_var B ${B:-"$A"}
29
30 expect '<aaa bbb ccc>'
31 recho "$A"
32 expect '<aaa bbb ccc>'
33 recho "$B"
34
35 eto_prepend() {
36 eval $1=\'$2\''${'$1':+":"${'$1'}}'; export $1
37 }
38
39 foo=bar; export foo
40 eto_prepend foo baz
41 expect '<baz:bar>'
42 recho $foo
43 expect '<baz:bar>'
44 recho ${foo-"bar"}
45
46 aa='aaa bbb ccc'
47
48 expect '<aaa bbb ccc>'
49 recho ${zzz-"$aa"}
50 expect '<bar>'
51 recho ${zzz:-"bar"}
52 expect '<bar>'
53 recho "${zzz:-bar}"
54 expect '<bar>'
55 recho "${zzz:-"bar"}"
56
57 var=abcde
58 expect '<abcde>'
59 recho "${var:-xyz}"
60 expect '<abcde>'
61 recho "${var:=xyz}"
62 expect '<xyz>'
63 recho "${var:+xyz}"
64
65 set 'a b' c d e f
66 expect '<a b> <c> <d> <e> <f>'
67 recho ${1+"$@"}
68 expect '<a b>'
69 recho "${1-"$@"}"
70 expect '<a> <b>'
71 recho ${1-"$@"}
72 expect '<a b> <c> <d> <e> <f>'
73 recho "${1+$@}"
74 expect '<a b> <c> <d> <e> <f>'
75 recho "${1+"$@"}"
76
77 HOME=/usr/homes/chet
78 somevar=
79 expect "<$HOME>"
80 recho ${somevar:-~}
81 # This changed after bash-3.0, when the tilde implementation was redone. It's
82 # not backward compatible, but it's very hard to be backward-compatible here,
83 # and I think the old behavior was a bug
84 expect '<~>'
85 recho "${somevar:-~}"
86 expect '<~>'
87 recho "${somevar:-"~"}"
88 expect '<\~>'
89 recho "${somevar:-\~}"
90 expect '<\ \~>'
91 recho "${somevar:-\ \~}"
92 expect '<\ \ \~>'
93 recho "${somevar:-\ \ \~}"
94
95 expect "<$HOME>"
96 recho ${somevar:-$HOME}
97 expect "<$HOME>"
98 recho "${somevar:-$HOME}"
99 expect "<$HOME>"
100 recho "${somevar:-"$HOME"}"
101 expect '<$HOME>'
102 recho "${somevar:-\$HOME}"
103 expect '<\ $HOME>'
104 recho "${somevar:-\ \$HOME}"
105 expect '<\ \ $HOME>'
106 recho "${somevar:-\ \ \$HOME}"
107
108 foo=bar
109 expect "<'bar'>"
110 recho "${foo+'$foo'}"
111 expect "<'bar'>"
112 recho "${fox='$foo'}"
113
114 P='*@*'
115 expect '<*@>'
116 recho "${P%"*"}"
117 expect '<*@>'
118 recho "${P%'*'}"
119
120 expect '<*@>'
121 recho ${P%"*"}
122 expect '<*@>'
123 recho ${P%'*'}
124
125 expect '<*@*>'
126 recho ${P%""}
127 expect '<*@*>'
128 recho ${P#""}
129
130 expect '<*@*>'
131 recho ${P#"$foobar"}
132 expect '<*@*>'
133 recho ${P%"$foobar"}
134
135 s1=abcdefghijkl
136 s2=efgh
137
138 first=${s1/$s2*/}
139 expect '<abcd>'
140 recho $first
141
142 last=${s1##$first}
143 expect '<efghijkl>'
144 recho $last
145
146 shift $#
147 UNAME_RELEASE=${1:-4.2MP}
148
149 RELEASE=`expr "$UNAME_RELEASE" : '[^0-9]*\([0-9]*\)'` # 4
150 case "$RELEASE" in
151 "") RELEASE=0 ;;
152 *) RELEASE=`expr "$RELEASE" + 0` ;;
153 esac
154 REL_LEVEL=`expr "$UNAME_RELEASE" : '[^0-9]*[0-9]*.\([0-9]*\)'` # 1
155 REL_SUBLEVEL=`expr "$UNAME_RELEASE" : '[^0-9]*[0-9]*.[0-9]*.\([0-9]*\)'` # 2
156
157 expect '<4> <2>'
158 recho $RELEASE $REL_LEVEL $REL_SUBLEVEL
159
160 b1()
161 {
162 b2 ${1+"$@"}
163 }
164
165 b2()
166 {
167 recho $*
168 recho ${#}
169 }
170
171 expect '<1>'
172 b1 ''
173
174 expect '<bar> <2>'
175 b1 bar ''
176
177 expect '<bar> <2>'
178 b1 '' bar
179
180 expect '<4>'
181 b1 '' '' '' ''
182
183 NL="\\
184 "
185
186 NNL="+$NL+"
187
188 expect '<--\> <-->'
189 recho --$NL--
190 expect '<--\^J-->'
191 recho "--$NL--"
192
193 expect '<--+\> <+-->'
194 recho --$NNL--
195 expect '<--+\^J+-->'
196 recho "--$NNL--"
197
198 expect '<-+\> <+-\> <->'
199 recho -$NNL-$NL-
200
201 set ''
202 expect '<xy>'
203 recho "$*xy"
204 expect '<xy>'
205 recho "x$*y"
206 expect '<xy>'
207 recho "xy$*"
208 expect '<xy>'
209 recho x"$*"y
210 expect '<xy>'
211 recho xy"$*"
212 expect '<xy>'
213 recho "$*"xy
214 expect '<>'
215 recho "$*"
216 expect nothing
217 recho $*
218
219 unset undef ; set ""
220
221 expect '<>'
222 recho ${undef-"$*"}
223 expect '<xy>'
224 recho ${undef-"x$*y"}
225 expect '<xy>'
226 recho ${undef-"$*xy"}
227 expect '<xy>'
228 recho ${undef-"xy$*"}
229 expect '<xy>'
230 recho ${undef-x"$*"y}
231 expect '<xy>'
232 recho ${undef-xy"$*"}
233 expect '<xy>'
234 recho ${undef-"$*"xy}
235 expect '<>'
236 recho "${undef-$*}"
237 expect nothing
238 recho ${undef-$*}
239
240 expect '<>'
241 recho ${undef-"$zzz"}
242 expect '<x>'
243 recho x${undef-"$zzz"}
244 expect '<x>'
245 recho x${undef-"$@"}
246 expect nothing
247 recho ${undef-"$@"}
248 expect '<x>'
249 recho ${undef-"$zzz"}x
250 expect '<x>'
251 recho ${undef-"$@"}x
252 expect '<x>'
253 recho "$@"x
254 expect '<x>'
255 recho "$zzz"x
256 expect '<^?>'
257 recho ${undef-\7f}
258 expect '<^?>'
259 recho ${undef-"\7f"}
260
261 yyy=""
262 recho "$xxx"x
263 recho "$yyy"x
264
265 set "" "abd" ""
266 recho "$@"x
267 recho "$@"$xxx
268
269 OIFS="$IFS"
270
271 arg=a,b,c,d,e,f
272
273 IFS=,
274
275 export z=$arg
276
277 eval z1=\"$arg\"
278
279 IFS="$OIFS"
280
281 recho $z
282 recho $z1
283
284 # should give an error
285 abc\=def
286
287 zz="a b c d e"
288 declare a=$zz
289
290 recho "$a"
291 recho $a
292
293 recho $(echo "foo$(echo ")")")
294
295 # test backslash escapes
296
297 recho \a
298 recho \\a
299
300 recho "\a"
301 recho "\\a"
302
303 recho '\a'
304 recho '\\a'
305
306 recho $(zecho \a)
307 recho $(zecho \\a)
308
309 recho $(zecho "\a")
310 recho $(zecho "\\a")
311
312 recho $(zecho '\a')
313 recho $(zecho '\\a')
314
315 recho `zecho \a`
316 recho `zecho \\a`
317
318 recho `zecho "\a"`
319 recho `zecho "\\a"`
320
321 recho `zecho '\a'`
322 recho `zecho '\\a'`
323
324 a=foo
325
326 recho \$a
327 recho \\$a
328
329 recho "\$a"
330 recho "\\$a"
331
332 recho '\$a'
333 recho '\\$a'
334
335 recho $(zecho `zecho \a`)
336 recho $(zecho `zecho \\a`)
337
338 recho $(zecho `zecho "\a"`)
339 recho $(zecho `zecho "\\a"`)
340
341 recho $(zecho `zecho '\a'`)
342 recho $(zecho `zecho '\\a'`)
343
344 # should echo G { I K }
345 recho ${abc:-G { I } K }
346
347 abc=hi
348
349 # should echo hi K }
350 recho ${abc:-G { I } K }
351
352 # should echo a*
353 unset foo
354 recho "${foo:-"a"}*"
355
356 f ()
357 {
358 echo "Number of args: $#"
359 echo "<\${*-x}>: <${*-x}>"
360 echo "<\${@-x}>: <${@-x}>"
361 }
362
363 f
364 f ''
365 f '' ''
366
367 set 1 2 3 4 5
368
369 expect '<5>'
370 recho ${#}
371 expect '<5>'
372 recho ${#:foo}
373 expect '<5>'
374 recho ${#:-foo}
375 expect '<5>'
376 recho ${#-posparams}
377 expect '<5>'
378 recho ${#:-posparams}
379
380 expect '<0>'
381 recho ${#!}
382
383 expect nothing
384 recho $!
385 expect nothing
386 recho ${!}
387
388 expect nothing
389 recho $8
390 expect nothing
391 recho ${8}
392
393 shift $#
394
395 expect '<0>'
396 recho ${#}
397 expect '<0>'
398 recho ${#:foo}
399 expect '<0>'
400 recho ${#:-foo}
401 expect '<0>'
402 recho ${#-posparams}
403 expect '<0>'
404 recho ${#:-posparams}
405
406 expect '<posparams>'
407 recho ${!-posparams}
408 expect '<posparams>'
409 recho ${!:-posparams}
410
411 expect '<2>'
412 recho ${#-}
413
414 expect '<0>'
415 recho ${#-posparams}
416
417 expect '<0>'
418 recho ${#?:-xyz}
419
420 expect '<1>'
421 recho ${#?}
422
423 set a b c d e
424
425 expect '<5>'
426 recho ${#}
427 expect '<5>'
428 recho ${#?:-xyz}
429
430 shift ${#}
431
432 expect '<0>'
433 recho ${#:-foo}
434
435 expect a bad substitution error
436 recho ${#:}
437 expect a bad substitution error
438 recho ${#/}
439 expect a bad substitution error
440 recho ${#%}
441 expect a bad substitution error
442 recho ${#=}
443 expect a bad substitution error
444 recho ${#+}
445 expect a bad substitution error
446 recho ${#1xyz}
447
448 expect a math syntax error
449 recho ${#:%}
450
451 expect '<0>'
452 recho ${#:-}
453
454 set --
455 unset a b
456
457 x=a
458 y=b
459
460 IFS=+
461
462 expect '<a+b>'
463 recho $x+$y
464 expect '<+>'
465 recho $a+$b
466
467 expect '<+>'
468 recho + "$@"
469 expect '<+>'
470 recho +"$@"
471
472 # variants of nested curly braces inside ${...} expressions
473
474 # IFS is not the standard one
475
476 expect '<G { I>' '<K>' '<}>'
477 recho ${gik:-G { I } K }
478
479 abc=hi
480
481 expect '<hi>' '<K>' '<}>'
482 recho ${abc:-G { I } K }
483
484 # reset IFS to the default
485 IFS='
486 '
487
488 # nested ${...} inside ${...} are handled specially
489 unset XXX FOO BAR
490 expect '<xxx>' '<yyy>'
491 XXX=xxx
492 FOO=${BAR:-${XXX} yyy}
493 recho $FOO
494
495 # this was a bug in versions of bash prior to bash-2.04-release
496 set -- ''
497 expect 1
498 echo $#
499 expect '<>'
500 recho "${@}"
501 expect '<>'
502 recho "${@-}"
503 expect '<>'
504 recho "${@:-}"
505
506 # this was a bug in bash-2.04, fixed in 2.05
507 set -- a b
508 expect '<:a:>' '<:b:>'
509 for i in "${@-}"; do recho :$i:; done
510
511 # I believe that ksh93 does these wrong -- we're using the rhs, so shouldn't
512 # it behave the same as ""?
513 set --
514 expect '<>'
515 recho "${@-}"
516 expect '<>'
517 recho "${@:-}"