281704192f014d5083fb8c6263e7dded7c38fd29
[platform/upstream/bash.git] / tests / new-exp.tests
1 # must do this because posix mode causes process substitution to be disabled
2 # and flagged as a syntax error, which causes the shell to exit
3 set +o posix
4
5 expect()
6 {
7         echo expect "$@"
8 }
9
10 HOME=/usr/homes/chet    # to make the check against new-exp.right work
11 expect '<foo bar>'
12 recho "${undef-"foo bar"}"      # should be foo bar
13 expect '<foo>'
14 recho "${und="foo"}"            # should be foo
15
16 expect "<$HOME>"
17 recho ${HOME-"}"}
18 expect "<$HOME>"
19 recho "${HOME-'}'}"
20 expect "<$HOME>"
21 recho "${HOME-"}"}"
22
23 expect $0: 'HOME: }: syntax error: operand expected (error token is "}")'
24 recho "${HOME:`echo }`}"        # should be a math error -- bad substring substitution
25
26 expect unset
27 _ENV=oops
28 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
29 echo ${x:-unset}
30
31 expect "<$HOME>"
32 recho ${HOME}
33 expect "<$HOME>"
34 recho ${HOME:-`echo }`}
35 expect "<$HOME>"
36 recho ${HOME:-`echo "}"`}
37 expect "<$HOME>"
38 recho "${HOME:-`echo "}"`}"
39 expect "<$HOME>"
40 recho "$(echo "${HOME}")"
41 expect "<$HOME>"
42 recho "$(echo "$(echo ${HOME})")"
43 expect "<$HOME>"
44 recho "$(echo "$(echo "${HOME}")")"
45
46 P=*@*
47 expect '<*@>'
48 recho "${P%"*"}"        # 
49 expect '<*@>'
50 recho "${P%'*'}"        # 
51 expect '<@*>'
52 recho "${P#\*}"         # should be @*
53
54 expect '<)>'
55 recho "$(echo ")")"     # should be )
56 expect '<")">'
57 recho "$(echo "\")\"")" # should be ")"
58
59 foo='abcd   '
60 expect '<-abcd> <->'
61 recho -${foo}-          # should be -abcd -
62 expect '<-abcd> <->'
63 recho -${foo% *}-       # should be -abcd -
64 expect '<-abcd->'
65 recho -${foo%% *}-      # should be -abcd-
66
67 foo=bar
68 expect '<bar foo>'
69 echo -n $foo' ' ; echo foo
70
71 expect '<bar foo>'
72 echo -n $foo" " ; echo foo
73
74 expect '<bar foo>'
75 echo -n "$foo " ; echo foo
76
77 expect '<barfoo>'
78 echo -e "$foo\c " ; echo foo
79
80 expect '<barfoo>'
81 echo -e $foo"\c " ; echo foo
82
83 # make sure backslashes are preserved in front of characters that are not
84 # valid backslash escapes
85 expect '<\x>'
86 echo -e '\x'
87
88 # substring tests
89 z=abcdefghijklmnop
90 expect '<abcd>'
91 recho ${z:0:4}
92
93 expect '<efg> <nop>'
94 recho ${z:4:3} ${z:${#z}-3:3}
95
96 expect '<efg> <nop>'
97 recho ${z:4:3} ${z: -3:3}
98
99 expect '<hijklmnop>'
100 recho ${z:7:30}
101
102 expect '<abcdefghijklmnop>'
103 recho ${z:0:100}
104
105 expect '<abcdefghijklmnop>'
106 recho ${z:0:${#z}}
107
108 set 'ab cd' 'ef' 'gh ij' 'kl mn' 'op'
109 expect '<ab cd> <ef>'
110 recho "${@:1:2}"
111
112 expect '<gh ij> <kl mn>'
113 recho "${@:3:2}"
114
115 expect '<gh ij> <kl mn> <op>'
116 recho "${@:3:4}"
117
118 expect '<ab cd> <ef> <gh ij> <kl mn> <op>'
119 recho "${@:1:$#}"
120
121 # code to ad-hoc parse arithmetic expressions in substring expansions was
122 # broken until post-2.04
123 base=/home/chet/foo//bar
124 string1=$base/abcabcabc
125 x=1 j=4
126
127 expect '</home/chet/foo//bar/abcabcabc>'
128 recho ${string1:0}
129
130 expect '<home/chet/foo//bar/abcabcabc>'
131 recho ${string1:1}
132
133 expect '<home>'
134 recho ${string1:(j?1:0):j}
135
136 expect '<home>'
137 recho ${string1:j?1:0:j}
138
139 expect '<home>'
140 recho ${string1:(j?(x?1:0):0):j}
141
142 expect '<home>'
143 recho ${string1:j?(x?1:0):0:j}
144
145 unset base string1 x j
146
147 # indirect variable references
148 expect '<abcdefghijklmnop>'
149 recho ${!9:-$z}
150
151 ef=4
152 expect '<4>'
153 recho ${!2}
154
155 expect '<op>'
156 recho ${!#}
157
158 set a b c d e
159 a=
160 expect '<abcdefghijklmnop>'
161 recho ${a:-$z}
162 expect '<abcdefghijklmnop>'
163 recho ${!1:-$z}
164
165 expect nothing
166 recho ${a-$z}
167 expect nothing
168 recho ${!1-$z}
169
170 set -u
171 expect $0: ABX: unbound variable
172 ( recho ${ABX} )
173 set +u
174
175 expect $0: '$6: cannot assign in this way'
176 recho ${6="arg6"}
177
178 v=abcde
179
180 # sed-like variable substitution
181 expect '<xxcde>'
182 recho ${v/a[a-z]/xx}
183 expect '<axxde>'
184 recho ${v/a??/axx}
185 expect '<abxyz>'
186 recho ${v/c??/xyz}
187 expect '<abbcde>'
188 recho ${v/#a/ab}
189 expect '<abcde>'
190 recho ${v/#d/ab}
191 expect '<abcabe>'
192 recho ${v/d/ab}
193 expect '<abcdlast>'
194 recho ${v/%?/last}
195 expect '<abcde>'
196 recho ${v/%x/last}
197
198 av=(abcd efgh ijkl mnop qrst uvwx)
199
200 expect '<xxcd>'
201 recho ${av/??/xx}
202 expect '<abxx>'
203 recho ${av/%??/xx}
204 expect '<xxgh>'
205 recho ${av[1]/??/xx}
206 expect '<efgh>'
207 recho ${av[1]/%ab/xx}
208 expect '<xxfgh>'
209 recho ${av[1]/#?/xx}
210 expect '<zagh>'
211 recho ${av[1]/??/za}
212 expect '<zaza>'
213 recho ${av[1]//??/za}
214 expect '<zagh>'
215 recho ${av[1]/#??/za}
216 expect '<efza>'
217 recho ${av[1]/%??/za}
218
219 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
220 recho ${av[@]/*/yyy}
221 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
222 recho ${av[@]/#*/yyy}
223 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
224 recho ${av[@]/%*/yyy}
225 expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
226 recho ${av[@]/a*/yyy}
227 expect '<abxx> <efxx> <ijxx> <mnxx> <qrxx> <uvxx>'
228 recho ${av[@]/%??/xx}
229
230 set abcd efgh ijkl mnop qrst uvwx
231
232 expect '<xxcd>'
233 recho ${1/??/xx}
234 expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
235 recho ${@/??/xx}
236 expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
237 recho ${@/%??/xx}
238 expect '<zaza>'
239 recho ${3//??/za}
240 expect '<efza>'
241 recho ${3/%??/za}
242 expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>'
243 recho ${@//??/za}
244 expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>'
245 recho ${@/#??/za}
246 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
247 recho ${@//*/yyy}
248 expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
249 recho ${@//a*/yyy}
250 expect '<abcd> <efgh> <ijkl> <mnop> <qrst> <uvwyyy>'
251 recho ${@/%x*/yyy}
252
253 expect a newline
254 echo $abmcde
255
256 # sneaky way to replace a newline in a variable value with something else
257 AVAR=$'This\nstring\nhas\nmultiple\nlines.'
258 echo "${AVAR}"
259
260 eval BVAR=\"\${AVAR//$'\n'/-}\"
261 echo "$BVAR"
262
263 unset AVAR BVAR
264
265 # run process substitution tests in a subshell so that syntax errors
266 # caused by a shell not implementing process substitution (e.g., one
267 # built on a NeXT) will not cause the whole test to exit prematurely
268 ${THIS_SH} ./new-exp1.sub
269
270 # run the tests of $(<filename) in a subshell to avoid cluttering up
271 # this script
272 ${THIS_SH} ./new-exp2.sub
273
274 expect '<6>'
275 recho ${#:-foo}
276 expect $0: '${#:}: bad substitution'
277 echo ${#:}
278
279 expect "<'>"
280 recho "'"
281 expect '<">'
282 recho '"'
283 expect '<"hello">'
284 recho "\"hello\""
285
286 shift $#
287 unset foo
288 z=abcdef
289 z1='abc def'
290
291 expect '<>'
292 recho ${foo:-""}
293 expect nothing
294 recho ${foo:-"$@"}
295 expect '<>'
296 recho "${foo:-$@}"
297
298 # unset var
299 expect '<>'
300 recho ${foo:-"$zbcd"}
301 expect nothing
302 recho ${foo:-$zbcd}
303
304 # set var
305 expect '<abcdef>'
306 recho ${foo:-"$z"}
307 expect '<abc def>'
308 recho ${foo:-"$z1"}
309
310 expect '<abcdef>'
311 recho ${foo:-$z}
312 expect '<abc> <def>'
313 recho ${foo:-$z1}
314
315 expect '<abcdef>'
316 recho "${foo:-$z}"
317 expect '<abc def>'
318 recho "${foo:-$z1}"
319
320 expect '<abcdef>'
321 recho "${foo:-"$z"}"
322 # this disagrees with sh and ksh, but I think it is right according
323 # to posix.2.
324 expect '<abc def>'
325 recho "${foo:-"$z1"}"
326
327 set ab cd ef gh
328 expect '<ab> <cd> <ef> <gh>'
329 recho ${foo:-"$@"}
330 expect '<ab> <cd> <ef> <gh>'
331 recho "${foo:-$@}"
332 expect '<ab> <cd> <ef> <gh>'
333 recho "${foo:-"$@"}"
334
335 shift $#
336 expect nothing
337 recho $xxx"$@"
338 expect nothing
339 recho ${foo:-$xxx"$@"}
340 expect '<>'
341 recho "${foo:-$xxx$@}"
342 expect '<>'
343 recho "${foo:-$xxx"$@"}"
344
345 expect nothing
346 recho $xxx"$@"
347 expect nothing
348 recho "$xxx$@"
349 expect nothing
350 recho "$@"$xxx
351
352 expect '<>'
353 recho $xxx""
354 expect '<>'
355 recho $xxx''
356 expect '<>'
357 recho ''$xxx
358 expect '<>'
359 recho ""$xxx
360
361 AB='abcdefghijklmnopqrstuvwxyz'
362
363 recho ${AB:7:15}
364 recho ${AB:15:7}
365
366 recho ${AB:20}
367
368 recho ${AB:0}
369 recho ${AB:0:20}
370
371 recho ${AB:10:7}
372 recho ${AB:10:3+4}
373 recho ${AB:20/2:3+4}
374
375 set 1 2 3 4 5 6
376 recho \""${*:2:2}"\"
377
378 IFS=:
379 recho \""${*:2:2}"\"
380
381 IFS=$' \t\n'
382
383 z=123456
384
385 recho \""${z:2:2}"\"
386 recho \""${z:2}"\"
387 recho \""${z:2:4}"\"
388 recho \""${z:2:6}"\"
389
390 set $'\1' $'\2' $'\177'
391
392 recho $*
393 recho $@
394
395 recho ${*}
396 recho ${@}
397
398 xx=one/two/two
399 recho ${xx%/*}
400 recho ${xx/\/two}
401
402 yy=oneonetwo
403 recho ${yy//one}
404 recho ${yy/\/one}
405
406 xx=oneonetwo
407
408 recho ${xx/one}
409 recho ${xx//one}
410 recho ${xx/\/one}
411
412 # out-of-range substrings
413 var=abc
414 c=${var:3}
415 expect nothing
416 recho $c
417 c=${var:4}
418 expect nothing
419 recho $c
420 # as of bash-4.2, negative LENGTH means offset from the end
421 c=${var:0:-2}
422 expect '<a>'
423 recho $c
424
425 var=abcdefghi
426 c=${var:3:12}
427 recho $c
428 c=${var:4:20}
429 recho $c
430
431 # make sure null patterns work
432 xxx=endocrine
433 yyy=n
434 unset zzz
435
436 recho ${xxx/$yyy/*}
437 recho ${xxx//$yyy/*}
438
439 recho ${xxx/$zzz/*}
440 recho ${xxx//$zzz/*}
441
442 recho ${xxx//%${zzz}/}
443 recho ${xxx//%${zzz}}
444 recho ${xxx//#${zzz}/}
445 recho ${xxx//#${zzz}}
446
447 # another case that caused a core dump in bash-2.0
448 XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin
449
450 recho ${XPATH//:/ }
451
452 xx=(ar as at au av aw ax ay az)
453
454 recho ${xx[@]/a/}
455 recho ${xx[@]//a/}
456
457 recho ${xx[*]/a/}
458 recho ${xx[*]//a/}
459
460 recho ${xx[@]%?}
461 recho ${xx[*]%?}
462
463 recho ${xx[@]#?}
464 recho ${xx[*]#?}
465
466 set -- ar as at au av aw ax ay az
467
468 recho ${@/a/}
469 recho ${@//a/}
470
471 recho ${*/a/}
472 recho ${*//a/}
473
474 recho ${@%?}
475 recho ${*%?}
476
477 recho ${@#?}
478 recho ${*#?}
479
480 shift $#
481 set -u
482 ( recho $9 ; echo after 1)
483 ( recho ${9} ; echo after 2)
484 ( recho $UNSET ; echo after 3)
485 ( recho ${UNSET} ; echo after 4)
486 ( recho "$UNSET" ; echo after 5)
487 ( recho "${UNSET}" ; echo after 6)
488 ( recho "${#UNSET}" ; echo after 7)
489 set +u
490
491 RECEIVED="12345"
492 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
493 RECEIVED="12345#"
494 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
495 RECEIVED="#"
496 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
497 RECEIVED=""
498 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
499
500 # tests of new prefix expansion ${!prefix*}
501 ${THIS_SH} ./new-exp3.sub
502
503 # bug with indirect expansion through bash-2.05b
504 ${THIS_SH} ./new-exp4.sub
505
506 # these caused errors and core dumps in versions before bash-2.04
507 c=""
508 echo ${c//${$(($#-1))}/x/}
509
510 set a b c d e f g
511 recho "$@"
512
513 set -- ${@:1:$(($# - 2))}
514 recho "$@"
515
516 set a b
517 recho ${@:1:$(($# - 2))}
518
519 recho ${@:1:0}
520 recho ${@:1:1}
521 recho ${@:1:2}
522
523 recho "${*:1:0}"
524
525 # this is an error -- negative expression
526 set a
527 recho ${@:1:$(($# - 2))}
528
529 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
530 set $( IFS=: ; echo $XPATH ) 
531
532 recho ${@##*/}
533 recho ${@%%[!/]*}
534
535 recho ${@#/*}
536 recho ${@%*/}
537
538 set /full/path/to/x16 /another/full/path
539
540 recho ${1%/*}
541 recho ${1%%[!/]*}
542 recho ${1#*/}
543 recho ${1##*/}
544
545 ${THIS_SH} ./new-exp5.sub
546
547 unset var
548 var=blah
549
550 # these had better agree
551 echo ${var[@]:3}
552 echo ${var:3}
553 echo ${var[@]/#/--}
554 echo ${var/#/--}
555 echo ${var[@]##?}
556 echo ${var##?}
557
558 unset var
559 var=(abcde abcfg abchi)
560
561 # problems with anchoring pattern replacements
562 echo ${var[*]//#abc/foo}
563 echo ${var[*]/#abc/foo}
564 unset var
565
566 ${THIS_SH} ./new-exp6.sub
567
568 ${THIS_SH} ./new-exp7.sub
569
570 ${THIS_SH} ./new-exp8.sub
571
572 # problems with stray CTLNUL in bash-4.0-alpha
573 unset a
574 a=/a
575 recho "/${a%/*}"
576 recho "/${a///a/}"
577
578 # this must be last!
579 expect $0: 'ABXD: parameter unset'
580 recho ${ABXD:?"parameter unset"}