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