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