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