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