]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/new-exp.tests
Imported from ../bash-2.04.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
121# indirect variable references
122expect '<abcdefghijklmnop>'
123recho ${!9:-$z}
124
125ef=4
126expect '<4>'
127recho ${!2}
128
129expect '<op>'
130recho ${!#}
131
132set a b c d e
133a=
134expect '<abcdefghijklmnop>'
135recho ${a:-$z}
136expect '<abcdefghijklmnop>'
137recho ${!1:-$z}
138
139expect nothing
140recho ${a-$z}
141expect nothing
142recho ${!1-$z}
143
726f6388
JA
144set -u
145expect $0: ABX: unbound variable
d166f048 146( recho ${ABX} )
726f6388
JA
147set +u
148
149expect $0: '$6: cannot assign in this way'
150recho ${6="arg6"}
151
ccc6cda3
JA
152v=abcde
153
154# sed-like variable substitution
155expect '<xxcde>'
156recho ${v/a[a-z]/xx}
157expect '<axxde>'
158recho ${v/a??/axx}
159expect '<abxyz>'
160recho ${v/c??/xyz}
161expect '<abbcde>'
162recho ${v/#a/ab}
163expect '<abcde>'
164recho ${v/#d/ab}
165expect '<abcabe>'
166recho ${v/d/ab}
167expect '<abcdlast>'
168recho ${v/%?/last}
169expect '<abcde>'
170recho ${v/%x/last}
171
172av=(abcd efgh ijkl mnop qrst uvwx)
173
174expect '<xxcd>'
175recho ${av/??/xx}
176expect '<abxx>'
177recho ${av/%??/xx}
178expect '<xxgh>'
179recho ${av[1]/??/xx}
180expect '<efgh>'
181recho ${av[1]/%ab/xx}
182expect '<xxfgh>'
183recho ${av[1]/#?/xx}
184expect '<zagh>'
185recho ${av[1]/??/za}
186expect '<zaza>'
187recho ${av[1]//??/za}
188expect '<zagh>'
189recho ${av[1]//#??/za}
190expect '<efza>'
191recho ${av[1]//%??/za}
192
193expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
194recho ${av[@]/*/yyy}
195expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
196recho ${av[@]/#*/yyy}
197expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
198recho ${av[@]/%*/yyy}
199expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
200recho ${av[@]/a*/yyy}
201expect '<abxx> <efxx> <ijxx> <mnxx> <qrxx> <uvxx>'
202recho ${av[@]/%??/xx}
203
204set abcd efgh ijkl mnop qrst uvwx
205
206expect '<xxcd>'
207recho ${1/??/xx}
208expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
209recho ${@/??/xx}
210expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
211recho ${@/%??/xx}
212expect '<zaza>'
213recho ${3//??/za}
214expect '<efza>'
215recho ${3//%??/za}
216expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>'
217recho ${@//??/za}
218expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>'
219recho ${@//#??/za}
220expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
221recho ${@//*/yyy}
222expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
223recho ${@//a*/yyy}
224expect '<abcd> <efgh> <ijkl> <mnop> <qrst> <uvwyyy>'
225recho ${@//%x*/yyy}
226
726f6388
JA
227expect a newline
228echo $abmcde
229
bb70624e
JA
230# sneaky way to replace a newline in a variable value with something else
231AVAR=$'This\nstring\nhas\nmultiple\nlines.'
232echo "${AVAR}"
233
234eval BVAR=\"\${AVAR//$'\n'/-}\"
235echo "$BVAR"
236
237unset AVAR BVAR
238
d166f048
JA
239# run process substitution tests in a subshell so that syntax errors
240# caused by a shell not implementing process substitution (e.g., one
241# built on a NeXT) will not cause the whole test to exit prematurely
cce855bc 242${THIS_SH} ./new-exp1.sub
ccc6cda3 243
cce855bc
JA
244# run the tests of $(<filename) in a subshell to avoid cluttering up
245# this script
246${THIS_SH} ./new-exp2.sub
247
248expect '<6>'
249recho ${#:-foo}
ccc6cda3
JA
250expect $0: '${#:}: bad substitution'
251echo ${#:}
252
253expect "<'>"
254recho "'"
255expect '<">'
256recho '"'
257expect '<"hello">'
258recho "\"hello\""
259
260shift $#
261unset foo
262z=abcdef
263z1='abc def'
264
265expect '<>'
266recho ${foo:-""}
267expect nothing
268recho ${foo:-"$@"}
269expect '<>'
270recho "${foo:-$@}"
271
272# unset var
273expect '<>'
274recho ${foo:-"$zbcd"}
275expect nothing
276recho ${foo:-$zbcd}
277
278# set var
279expect '<abcdef>'
280recho ${foo:-"$z"}
281expect '<abc def>'
282recho ${foo:-"$z1"}
283
284expect '<abcdef>'
285recho ${foo:-$z}
286expect '<abc> <def>'
287recho ${foo:-$z1}
288
289expect '<abcdef>'
290recho "${foo:-$z}"
291expect '<abc def>'
292recho "${foo:-$z1}"
293
294expect '<abcdef>'
295recho "${foo:-"$z"}"
296# this disagrees with sh and ksh, but I think it is right according
297# to posix.2.
298expect '<abc def>'
299recho "${foo:-"$z1"}"
300
301set ab cd ef gh
302expect '<ab> <cd> <ef> <gh>'
303recho ${foo:-"$@"}
304expect '<ab> <cd> <ef> <gh>'
305recho "${foo:-$@}"
306expect '<ab> <cd> <ef> <gh>'
307recho "${foo:-"$@"}"
308
309shift $#
310expect nothing
311recho $xxx"$@"
312expect nothing
313recho ${foo:-$xxx"$@"}
314expect '<>'
315recho "${foo:-$xxx$@}"
316expect '<>'
317recho "${foo:-$xxx"$@"}"
318
319expect nothing
320recho $xxx"$@"
321expect nothing
322recho "$xxx$@"
323expect nothing
324recho "$@"$xxx
325
326expect '<>'
327recho $xxx""
328expect '<>'
329recho $xxx''
330expect '<>'
331recho ''$xxx
332expect '<>'
333recho ""$xxx
334
335AB='abcdefghijklmnopqrstuvwxyz'
336
337recho ${AB:7:15}
338recho ${AB:15:7}
339
340recho ${AB:20}
341
342recho ${AB:0}
343recho ${AB:0:20}
344
345recho ${AB:10:7}
346recho ${AB:10:3+4}
347recho ${AB:20/2:3+4}
348
349set 1 2 3 4 5 6
350recho \""${*:2:2}"\"
351
352IFS=:
353recho \""${*:2:2}"\"
354
355IFS=$' \t\n'
356
357z=123456
358
359recho \""${z:2:2}"\"
360recho \""${z:2}"\"
361recho \""${z:2:4}"\"
362recho \""${z:2:6}"\"
363
364set $'\1' $'\2' $'\177'
365
366recho $*
367recho $@
368
369recho ${*}
370recho ${@}
371
372xx=one/two/two
373recho ${xx%/*}
374recho ${xx/\/two}
375
376yy=oneonetwo
377recho ${yy//one}
378recho ${yy/\/one}
379
380xx=oneonetwo
381
382recho ${xx/one}
383recho ${xx//one}
384recho ${xx/\/one}
385
d166f048
JA
386# out-of-range substrings
387var=abc
388c=${var:3}
389expect nothing
390recho $c
391c=${var:4}
392expect nothing
393recho $c
394expect '<./new-exp.tests: -2: substring expression < 0>'
395c=${var:0:-2}
396
397var=abcdefghi
398c=${var:3:12}
399recho $c
400c=${var:4:20}
401recho $c
402
403# make sure null patterns work
404xxx=endocrine
405yyy=n
406unset zzz
407
408recho ${xxx/$yyy/*}
409recho ${xxx//$yyy/*}
410
411recho ${xxx/$zzz/*}
412recho ${xxx//$zzz/*}
413
bb70624e
JA
414recho ${xxx//%${zzz}/}
415recho ${xxx//%${zzz}}
416recho ${xxx//#${zzz}/}
417recho ${xxx//#${zzz}}
418
d166f048
JA
419# another case that caused a core dump in bash-2.0
420XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin
421
422recho ${XPATH//:/ }
423
424xx=(ar as at au av aw ax ay az)
425
426recho ${xx[@]/a/}
427recho ${xx[@]//a/}
428
429recho ${xx[*]/a/}
430recho ${xx[*]//a/}
431
432recho ${xx[@]%?}
433recho ${xx[*]%?}
434
435recho ${xx[@]#?}
436recho ${xx[*]#?}
437
438set -- ar as at au av aw ax ay az
439
440recho ${@/a/}
441recho ${@//a/}
442
443recho ${*/a/}
444recho ${*//a/}
445
446recho ${@%?}
447recho ${*%?}
448
449recho ${@#?}
450recho ${*#?}
451
452shift $#
453set -u
454( recho $9 ; echo after 1)
455( recho ${9} ; echo after 2)
456( recho $UNSET ; echo after 3)
457( recho ${UNSET} ; echo after 4)
458( recho "$UNSET" ; echo after 5)
459( recho "${UNSET}" ; echo after 6)
460( recho "${#UNSET}" ; echo after 7)
461set +u
462
cce855bc
JA
463RECEIVED="12345"
464recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
465RECEIVED="12345#"
466recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
467RECEIVED="#"
468recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
469RECEIVED=""
470recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
471
bb70624e
JA
472# tests of new prefix expansion ${!prefix*}
473${THIS_SH} ./new-exp3.sub
474
475# these caused errors and core dumps in versions before bash-2.04
476c=""
477echo ${c//${$(($#-1))}/x/}
478
479set a b c d e f g
480recho "$@"
481
482set -- ${@:1:$(($# - 2))}
483recho "$@"
484
485set a b
486recho ${@:1:$(($# - 2))}
487
488recho ${@:1:0}
489recho ${@:1:1}
490recho ${@:1:2}
491
492recho "${*:1:0}"
493
494# this is an error -- negative expression
495set a
496recho ${@:1:$(($# - 2))}
497
726f6388
JA
498# this must be last!
499expect $0: 'ABXD: parameter unset'
500recho ${ABXD:?"parameter unset"}