]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/new-exp.tests
Imported from ../bash-2.05.tar.gz.
[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=*@*
ccc6cda3
JA
47expect '<*@*>'
48recho "${P%"*"}" #
49expect '<*@*>'
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>'
215recho ${av[1]//#??/za}
216expect '<efza>'
217recho ${av[1]//%??/za}
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>'
241recho ${3//%??/za}
242expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>'
243recho ${@//??/za}
244expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>'
245recho ${@//#??/za}
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>'
251recho ${@//%x*/yyy}
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
420expect '<./new-exp.tests: -2: substring expression < 0>'
421c=${var:0:-2}
422
423var=abcdefghi
424c=${var:3:12}
425recho $c
426c=${var:4:20}
427recho $c
428
429# make sure null patterns work
430xxx=endocrine
431yyy=n
432unset zzz
433
434recho ${xxx/$yyy/*}
435recho ${xxx//$yyy/*}
436
437recho ${xxx/$zzz/*}
438recho ${xxx//$zzz/*}
439
bb70624e
JA
440recho ${xxx//%${zzz}/}
441recho ${xxx//%${zzz}}
442recho ${xxx//#${zzz}/}
443recho ${xxx//#${zzz}}
444
d166f048
JA
445# another case that caused a core dump in bash-2.0
446XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin
447
448recho ${XPATH//:/ }
449
450xx=(ar as at au av aw ax ay az)
451
452recho ${xx[@]/a/}
453recho ${xx[@]//a/}
454
455recho ${xx[*]/a/}
456recho ${xx[*]//a/}
457
458recho ${xx[@]%?}
459recho ${xx[*]%?}
460
461recho ${xx[@]#?}
462recho ${xx[*]#?}
463
464set -- ar as at au av aw ax ay az
465
466recho ${@/a/}
467recho ${@//a/}
468
469recho ${*/a/}
470recho ${*//a/}
471
472recho ${@%?}
473recho ${*%?}
474
475recho ${@#?}
476recho ${*#?}
477
478shift $#
479set -u
480( recho $9 ; echo after 1)
481( recho ${9} ; echo after 2)
482( recho $UNSET ; echo after 3)
483( recho ${UNSET} ; echo after 4)
484( recho "$UNSET" ; echo after 5)
485( recho "${UNSET}" ; echo after 6)
486( recho "${#UNSET}" ; echo after 7)
487set +u
488
cce855bc
JA
489RECEIVED="12345"
490recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
491RECEIVED="12345#"
492recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
493RECEIVED="#"
494recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
495RECEIVED=""
496recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
497
bb70624e
JA
498# tests of new prefix expansion ${!prefix*}
499${THIS_SH} ./new-exp3.sub
500
501# these caused errors and core dumps in versions before bash-2.04
502c=""
503echo ${c//${$(($#-1))}/x/}
504
505set a b c d e f g
506recho "$@"
507
508set -- ${@:1:$(($# - 2))}
509recho "$@"
510
511set a b
512recho ${@:1:$(($# - 2))}
513
514recho ${@:1:0}
515recho ${@:1:1}
516recho ${@:1:2}
517
518recho "${*:1:0}"
519
520# this is an error -- negative expression
521set a
522recho ${@:1:$(($# - 2))}
523
726f6388
JA
524# this must be last!
525expect $0: 'ABXD: parameter unset'
526recho ${ABXD:?"parameter unset"}