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