]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/array.tests
Imported from ../bash-3.2.48.tar.gz.
[thirdparty/bash.git] / tests / array.tests
CommitLineData
d166f048
JA
1# this is needed so that the bad assignments (b[]=bcde, for example) do not
2# cause fatal shell errors when in posix mode
3set +o posix
4
ccc6cda3
JA
5set +a
6# The calls to egrep -v are to filter out builtin array variables that are
7# automatically set and possibly contain values that vary.
d166f048 8
7117c2d2
JA
9# first make sure we handle the basics
10x=()
11echo ${x[@]}
12unset x
13
bb70624e
JA
14# this should be an error
15test=(first & second)
16echo $?
17unset test
18
d166f048
JA
19# make sure declare -a converts an existing variable to an array
20unset a
21a=abcde
22declare -a a
23echo ${a[0]}
24
ccc6cda3
JA
25unset a
26a=abcde
27a[2]=bdef
28
f73dda09 29unset b
ccc6cda3
JA
30declare -a b[256]
31
32unset c[2]
33unset c[*]
34
35a[1]=
36
37_ENV=/bin/true
38x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
39
40declare -r c[100]
41
42echo ${a[0]} ${a[4]}
43echo ${a[@]}
44
45echo ${a[*]}
46
47# this should print out values, too
d166f048 48declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
49
50unset a[7]
51echo ${a[*]}
52
53unset a[4]
54echo ${a[*]}
55
56echo ${a}
57echo "${a}"
58echo $a
59
60unset a[0]
61echo ${a}
62
63echo ${a[@]}
64
65a[5]="hello world"
66echo ${a[5]}
67echo ${#a[5]}
68
69echo ${#a[@]}
70
71a[4+5/2]="test expression"
72echo ${a[@]}
73
74readonly a[5]
75readonly a
cce855bc 76# these two lines should output `declare' commands
d166f048
JA
77readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
78declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
cce855bc
JA
79# this line should output `readonly' commands, even for arrays
80set -o posix
81readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
82set +o posix
ccc6cda3
JA
83
84declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
85d[9]="ninth element"
86
f73dda09 87declare -a e[10]=test # this works in post-bash-2.05 versions
ccc6cda3
JA
88declare -a e[10]='(test)'
89
90pass=/etc/passwd
91declare -a f='("${d[@]}")'
92b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
93
94echo ${b[@]:2:3}
95
d166f048 96declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
97
98a[3]="this is a test"
99
100b[]=bcde
101b[*]=aaa
102echo ${b[ ]}
103
104c[-2]=4
105echo ${c[-4]}
106
107d[7]=(abdedfegeee)
108
109d=([]=abcde [1]="test test" [*]=last [-65]=negative )
110
111unset d[12]
112unset e[*]
113
d166f048 114declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
115
116ps1='hello'
117unset ps1[2]
118unset ${ps1[2]}
119
120declare +a ps1
121declare +a c
122
123# the prompt should not print when using a here doc
124read -p "array test: " -a rv <<!
125this is a test of read using arrays
126!
127
128echo ${rv[0]} ${rv[4]}
129echo ${rv[@]}
130
cce855bc
JA
131# the variable should be converted to an array when `read -a' is done
132vv=1
133read -a vv <<!
134this is a test of arrays
135!
136echo ${vv[0]} ${vv[3]}
137echo ${vv[@]}
138unset vv
139
d166f048 140declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
141
142export rv
143#set
144
145x[4]=bbb
146x=abde
147echo $x
148echo ${x[0]}
149echo ${x[4]}
150echo efgh | ( read x[1] ; echo ${x[1]} )
151echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
152
153# Make sure that arrays can be used to save the positional paramters verbatim
154set -- a 'b c' d 'e f g' h
155
156ARGV=( [0]=$0 "$@" )
157
158for z in "${ARGV[@]}"
159do
160 echo "$z"
161done
162
163echo "$0"
164for z in "$@"
165do
166 echo "$z"
167done
d166f048
JA
168
169# do various pattern removal and length tests
170XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
171
172xpath=( $( IFS=: ; echo $XPATH ) )
173
174echo ${xpath[@]}
175echo ${xpath[@]##*/}
176echo ${xpath[0]##*/}
177echo ${xpath[@]%%[!/]*}
178echo ${xpath[0]%%[!/]*}
7117c2d2
JA
179recho ${xpath##*/}
180recho ${xpath%%[!/]*}
181recho ${xpath[5]##*/}
182recho ${xpath[5]%%[!/]*}
d166f048
JA
183
184# let's try to make it a DOS-style path
185
186zecho "${xpath[@]/\//\\}"
187zecho "${xpath[@]//\//\\}"
188zecho "${xpath[@]//[\/]/\\}"
189
190# length of the first element of the array, since array without subscript
191# is equivalent to referencing first element
192echo ${#xpath} -- ${#xpath[0]}
193
194# number of elements in the array
195nelem=${#xpath[@]}
196echo ${#xpath[@]} -- $nelem
197
198# total length of all elements in the array, including space separators
199xx="${xpath[*]}"
200echo ${#xx}
201
202# total length of all elements in the array
203xx=$( IFS='' ; echo "${xpath[*]}" )
204echo ${#xx}
205
206unset xpath[nelem-1]
207
208nelem=${#xpath[@]}
209echo ${#xpath[@]} -- $nelem
210
211# arrays and things that look like index assignments
212array=(42 [1]=14 [2]=44)
213
214array2=(grep [ 123 ] \*)
215
216echo ${array[@]}
217echo "${array2[@]}"
218
219# arrays and implicit arithmetic evaluation
220declare -i -a iarray
221
222iarray=( 2+4 1+6 7+2 )
223echo ${iarray[@]}
224
225iarray[4]=4+1
226echo ${iarray[@]}
227
cce855bc
JA
228# make sure assignment using the compound assignment syntax removes all
229# of the old elements from the array value
230barray=(old1 old2 old3 old4 old5)
231barray=(new1 new2 new3)
232echo "length = ${#barray[@]}"
233echo "value = ${barray[*]}"
234
d166f048
JA
235# make sure the array code behaves correctly with respect to unset variables
236set -u
237( echo ${#narray[4]} )
bb70624e 238
95732b49
JA
239${THIS_SH} ./array1.sub
240${THIS_SH} ./array2.sub
241
bb70624e 242# some old bugs and ksh93 compatibility tests
95732b49
JA
243${THIS_SH} ./array3.sub
244
0628567a
JA
245# some compound assingment parsing problems that showed up in bash-3.1-release
246${THIS_SH} ./array4.sub
247
bb70624e
JA
248set +u
249cd /tmp
250
251touch 1=bar
252foo=([10]="bar")
253echo ${foo[0]}
254rm 1=bar
255
256foo=(a b c d e f g)
257echo ${foo[@]}
258
259# quoted reserved words are ok
260foo=(\for \case \if \then \else)
261echo ${foo[@]}
262
263# quoted metacharacters are ok
264foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
265echo ${foo[@]}
266
267# numbers are just words when not in a redirection context
268foo=( 12 14 16 18 20 )
269echo ${foo[@]}
270
271foo=( 4414758999202 )
272echo ${foo[@]}
273
28ef6c31
JA
274# this was a bug in all versions of bash 2.x up to and including bash-2.04
275declare -a ddd=(aaa
276bbb)
277echo ${ddd[@]}
278
7117c2d2 279# errors until post-bash-2.05a; now reserved words are OK
bb70624e
JA
280foo=(a b c for case if then else)
281
282foo=(for case if then else)
283
7117c2d2 284# errors
bb70624e
JA
285metas=( <> < > ! )
286metas=( [1]=<> [2]=< [3]=> [4]=! )
28ef6c31
JA
287
288# various expansions that didn't really work right until post-bash-2.04
289foo='abc'
290echo ${foo[0]} ${#foo[0]}
291echo ${foo[1]} ${#foo[1]}
292echo ${foo[@]} ${#foo[@]}
293echo ${foo[*]} ${#foo[*]}
294
295foo=''
296echo ${foo[0]} ${#foo[0]}
297echo ${foo[1]} ${#foo[1]}
298echo ${foo[@]} ${#foo[@]}
299echo ${foo[*]} ${#foo[*]}
b80f6443
JA
300
301# new expansions added after bash-2.05b
302x[0]=zero
303x[1]=one
304x[4]=four
305x[10]=ten
306
307recho ${!x[@]}
308recho "${!x[@]}"
309recho ${!x[*]}
310recho "${!x[*]}"
311
312# sparse array tests for code fixed in bash-3.0
313unset av
314av[1]='one'
315av[2]=''
316
317av[3]=three
318av[5]=five
319av[7]=seven
320
321echo include null element -- expect one
322echo ${av[@]:1:2} # what happens when we include a null element?
323echo include unset element -- expect three five
324echo ${av[@]:3:2} # what happens when we include an unset element?
325echo start at unset element -- expect five seven
326echo ${av[@]:4:2} # what happens when we start at an unset element?
327
328echo too many elements -- expect three five seven
329echo ${av[@]:3:5} # how about too many elements?
330
331echo positive offset - expect five seven
332echo ${av[@]:5:2}
eb873671 333echo negative offset to unset element - expect seven
b80f6443
JA
334echo ${av[@]: -2:2}
335
336echo positive offset 2 - expect seven
337echo ${av[@]: 6:2}
338echo negative offset 2 - expect seven
339echo ${av[@]: -1:2}
340
341echo out-of-range offset
342echo ${av[@]:12}
95732b49
JA
343
344# parsing problems and other inconsistencies not fixed until post bash-3.0
345unset x
346declare -a x=(')' $$)
347[ ${x[1]} -eq $$ ] || echo bad
348
349unset x
350declare -a x=(a b c d e)
351echo ${x[4]}
352
353z=([1]=one [4]=four [7]=seven [10]=ten)
354
355echo ${#z[@]}
356
357echo ${!z[@]}
358
359unset x
360declare -a x=(a \'b c\')
361
362echo "${x[1]}"
363
364unset x
365declare -a x=(a 'b c')
366
367echo "${x[1]}"
368
369unset x
370declare -a x=($0)
371[ "${x[@]}" = $0 ] || echo double expansion of \$0
372declare -a x=(\$0)
373echo "${x[@]}"
374
375: ${TMPDIR:=/tmp}
376
377mkdir $TMPDIR/bash-test-$$
378cd $TMPDIR/bash-test-$$
379
380trap "cd / ; rm -rf $TMPDIR/bash-test-$$" 0 1 2 3 6 15
381
382touch '[3]=abcde'
383
384touch r s t u v
385
386declare -a x=(*)
387
388echo ${x[3]}
389echo ${x[@]}
390
391unset x
392x=(a b c d e)
393
394echo ${x[*]: -1}
395
396unset x[4]
397unset x[2]
398
399x[9]='9'
400
401echo ${x[*]: -1}
0628567a
JA
402
403TOOLKIT=(1 2 3 4 5 6 7 8 9 10)
404ARRAY="1"
405echo ${TOOLKIT["$ARRAY"]}