]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/array.tests
Imported from ../bash-2.04.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
bb70624e
JA
9# this should be an error
10test=(first & second)
11echo $?
12unset test
13
d166f048
JA
14# make sure declare -a converts an existing variable to an array
15unset a
16a=abcde
17declare -a a
18echo ${a[0]}
19
ccc6cda3
JA
20unset a
21a=abcde
22a[2]=bdef
23
24declare -a b[256]
25
26unset c[2]
27unset c[*]
28
29a[1]=
30
31_ENV=/bin/true
32x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
33
34declare -r c[100]
35
36echo ${a[0]} ${a[4]}
37echo ${a[@]}
38
39echo ${a[*]}
40
41# this should print out values, too
d166f048 42declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
43
44unset a[7]
45echo ${a[*]}
46
47unset a[4]
48echo ${a[*]}
49
50echo ${a}
51echo "${a}"
52echo $a
53
54unset a[0]
55echo ${a}
56
57echo ${a[@]}
58
59a[5]="hello world"
60echo ${a[5]}
61echo ${#a[5]}
62
63echo ${#a[@]}
64
65a[4+5/2]="test expression"
66echo ${a[@]}
67
68readonly a[5]
69readonly a
cce855bc 70# these two lines should output `declare' commands
d166f048
JA
71readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
72declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
cce855bc
JA
73# this line should output `readonly' commands, even for arrays
74set -o posix
75readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
76set +o posix
ccc6cda3
JA
77
78declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
79d[9]="ninth element"
80
81declare -a e[10]=test
82declare -a e[10]='(test)'
83
84pass=/etc/passwd
85declare -a f='("${d[@]}")'
86b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
87
88echo ${b[@]:2:3}
89
d166f048 90declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
91
92a[3]="this is a test"
93
94b[]=bcde
95b[*]=aaa
96echo ${b[ ]}
97
98c[-2]=4
99echo ${c[-4]}
100
101d[7]=(abdedfegeee)
102
103d=([]=abcde [1]="test test" [*]=last [-65]=negative )
104
105unset d[12]
106unset e[*]
107
d166f048 108declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
109
110ps1='hello'
111unset ps1[2]
112unset ${ps1[2]}
113
114declare +a ps1
115declare +a c
116
117# the prompt should not print when using a here doc
118read -p "array test: " -a rv <<!
119this is a test of read using arrays
120!
121
122echo ${rv[0]} ${rv[4]}
123echo ${rv[@]}
124
cce855bc
JA
125# the variable should be converted to an array when `read -a' is done
126vv=1
127read -a vv <<!
128this is a test of arrays
129!
130echo ${vv[0]} ${vv[3]}
131echo ${vv[@]}
132unset vv
133
d166f048 134declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
ccc6cda3
JA
135
136export rv
137#set
138
139x[4]=bbb
140x=abde
141echo $x
142echo ${x[0]}
143echo ${x[4]}
144echo efgh | ( read x[1] ; echo ${x[1]} )
145echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
146
147# Make sure that arrays can be used to save the positional paramters verbatim
148set -- a 'b c' d 'e f g' h
149
150ARGV=( [0]=$0 "$@" )
151
152for z in "${ARGV[@]}"
153do
154 echo "$z"
155done
156
157echo "$0"
158for z in "$@"
159do
160 echo "$z"
161done
d166f048
JA
162
163# do various pattern removal and length tests
164XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
165
166xpath=( $( IFS=: ; echo $XPATH ) )
167
168echo ${xpath[@]}
169echo ${xpath[@]##*/}
170echo ${xpath[0]##*/}
171echo ${xpath[@]%%[!/]*}
172echo ${xpath[0]%%[!/]*}
173
174# let's try to make it a DOS-style path
175
176zecho "${xpath[@]/\//\\}"
177zecho "${xpath[@]//\//\\}"
178zecho "${xpath[@]//[\/]/\\}"
179
180# length of the first element of the array, since array without subscript
181# is equivalent to referencing first element
182echo ${#xpath} -- ${#xpath[0]}
183
184# number of elements in the array
185nelem=${#xpath[@]}
186echo ${#xpath[@]} -- $nelem
187
188# total length of all elements in the array, including space separators
189xx="${xpath[*]}"
190echo ${#xx}
191
192# total length of all elements in the array
193xx=$( IFS='' ; echo "${xpath[*]}" )
194echo ${#xx}
195
196unset xpath[nelem-1]
197
198nelem=${#xpath[@]}
199echo ${#xpath[@]} -- $nelem
200
201# arrays and things that look like index assignments
202array=(42 [1]=14 [2]=44)
203
204array2=(grep [ 123 ] \*)
205
206echo ${array[@]}
207echo "${array2[@]}"
208
209# arrays and implicit arithmetic evaluation
210declare -i -a iarray
211
212iarray=( 2+4 1+6 7+2 )
213echo ${iarray[@]}
214
215iarray[4]=4+1
216echo ${iarray[@]}
217
cce855bc
JA
218# make sure assignment using the compound assignment syntax removes all
219# of the old elements from the array value
220barray=(old1 old2 old3 old4 old5)
221barray=(new1 new2 new3)
222echo "length = ${#barray[@]}"
223echo "value = ${barray[*]}"
224
d166f048
JA
225# make sure the array code behaves correctly with respect to unset variables
226set -u
227( echo ${#narray[4]} )
bb70624e
JA
228
229# some old bugs and ksh93 compatibility tests
230set +u
231cd /tmp
232
233touch 1=bar
234foo=([10]="bar")
235echo ${foo[0]}
236rm 1=bar
237
238foo=(a b c d e f g)
239echo ${foo[@]}
240
241# quoted reserved words are ok
242foo=(\for \case \if \then \else)
243echo ${foo[@]}
244
245# quoted metacharacters are ok
246foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
247echo ${foo[@]}
248
249# numbers are just words when not in a redirection context
250foo=( 12 14 16 18 20 )
251echo ${foo[@]}
252
253foo=( 4414758999202 )
254echo ${foo[@]}
255
256# errors
257foo=(a b c for case if then else)
258
259foo=(for case if then else)
260
261metas=( <> < > ! )
262metas=( [1]=<> [2]=< [3]=> [4]=! )