1 # this is needed so that the bad assignments (b[]=bcde, for example) do not
2 # cause fatal shell errors when in posix mode
6 # The calls to egrep -v are to filter out builtin array variables that are
7 # automatically set and possibly contain values that vary.
9 # first make sure we handle the basics
14 # this should be an error
19 # make sure declare -a converts an existing variable to an array
38 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
47 # this should print out values, too
48 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
71 a[4+5/2]="test expression"
72 declare a["7 + 8"]="test 2"
78 # these two lines should output `declare' commands
79 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
80 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
81 # this line should output `readonly' commands, even for arrays
83 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
86 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
89 declare -a e[10]=test # this works in post-bash-2.05 versions
90 declare -a e[10]='(test)'
93 declare -a f='("${d[@]}")'
94 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
98 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
100 a[3]="this is a test"
111 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
116 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
125 # the prompt should not print when using a here doc
126 read -p "array test: " -a rv <<!
127 this is a test of read using arrays
130 echo ${rv[0]} ${rv[4]}
133 # the variable should be converted to an array when `read -a' is done
136 this is a test of arrays
138 echo ${vv[0]} ${vv[3]}
142 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
152 echo efgh | ( read x[1] ; echo ${x[1]} )
153 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
155 # Make sure that arrays can be used to save the positional paramters verbatim
156 set -- a 'b c' d 'e f g' h
160 for z in "${ARGV[@]}"
171 # do various pattern removal and length tests
172 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
174 xpath=( $( IFS=: ; echo $XPATH ) )
179 echo ${xpath[@]%%[!/]*}
180 echo ${xpath[0]%%[!/]*}
182 recho ${xpath%%[!/]*}
183 recho ${xpath[5]##*/}
184 recho ${xpath[5]%%[!/]*}
186 # let's try to make it a DOS-style path
188 zecho "${xpath[@]/\//\\}"
189 zecho "${xpath[@]//\//\\}"
190 zecho "${xpath[@]//[\/]/\\}"
192 # length of the first element of the array, since array without subscript
193 # is equivalent to referencing first element
194 echo ${#xpath} -- ${#xpath[0]}
196 # number of elements in the array
198 echo ${#xpath[@]} -- $nelem
200 # total length of all elements in the array, including space separators
204 # total length of all elements in the array
205 xx=$( IFS='' ; echo "${xpath[*]}" )
211 echo ${#xpath[@]} -- $nelem
213 # arrays and things that look like index assignments
214 array=(42 [1]=14 [2]=44)
216 array2=(grep [ 123 ] \*)
221 # arrays and implicit arithmetic evaluation
224 iarray=( 2+4 1+6 7+2 )
230 # make sure assignment using the compound assignment syntax removes all
231 # of the old elements from the array value
232 barray=(old1 old2 old3 old4 old5)
233 barray=(new1 new2 new3)
234 echo "length = ${#barray[@]}"
235 echo "value = ${barray[*]}"
237 # make sure the array code behaves correctly with respect to unset variables
239 ( echo ${#narray[4]} )
241 ${THIS_SH} ./array1.sub
242 ${THIS_SH} ./array2.sub
244 # some old bugs and ksh93 compatibility tests
245 ${THIS_SH} ./array3.sub
247 # some compound assingment parsing problems that showed up in bash-3.1-release
248 ${THIS_SH} ./array4.sub
263 # quoted reserved words are ok
264 foo=(\for \case \if \then \else)
267 # quoted metacharacters are ok
268 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
271 # numbers are just words when not in a redirection context
272 foo=( 12 14 16 18 20 )
275 foo=( 4414758999202 )
278 # this was a bug in all versions of bash 2.x up to and including bash-2.04
283 # errors until post-bash-2.05a; now reserved words are OK
284 foo=(a b c for case if then else)
286 foo=(for case if then else)
290 metas=( [1]=<> [2]=< [3]=> [4]=! )
292 # various expansions that didn't really work right until post-bash-2.04
294 echo ${foo[0]} ${#foo[0]}
295 echo ${foo[1]} ${#foo[1]}
296 echo ${foo[@]} ${#foo[@]}
297 echo ${foo[*]} ${#foo[*]}
300 echo ${foo[0]} ${#foo[0]}
301 echo ${foo[1]} ${#foo[1]}
302 echo ${foo[@]} ${#foo[@]}
303 echo ${foo[*]} ${#foo[*]}
305 # new expansions added after bash-2.05b
316 # sparse array tests for code fixed in bash-3.0
325 echo include null element -- expect one
326 echo ${av[@]:1:2} # what happens when we include a null element?
327 echo include unset element -- expect three five
328 echo ${av[@]:3:2} # what happens when we include an unset element?
329 echo start at unset element -- expect five seven
330 echo ${av[@]:4:2} # what happens when we start at an unset element?
332 echo too many elements -- expect three five seven
333 echo ${av[@]:3:5} # how about too many elements?
335 echo positive offset - expect five seven
337 echo negative offset to unset element - expect seven
340 echo positive offset 2 - expect seven
342 echo negative offset 2 - expect seven
345 echo out-of-range offset
348 # parsing problems and other inconsistencies not fixed until post bash-3.0
350 declare -a x=(')' $$)
351 [ ${x[1]} -eq $$ ] || echo bad
354 declare -a x=(a b c d e)
357 z=([1]=one [4]=four [7]=seven [10]=ten)
364 declare -a x=(a \'b c\')
369 declare -a x=(a 'b c')
375 [ "${x[@]}" = $0 ] || echo double expansion of \$0
379 # tests for bash-3.1 problems
380 ${THIS_SH} ./array5.sub
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
386 ${THIS_SH} ./array8.sub
388 ${THIS_SH} ./array9.sub
390 ${THIS_SH} ./array10.sub
392 ${THIS_SH} ./array11.sub
394 ${THIS_SH} ./array12.sub
396 ${THIS_SH} ./array13.sub
398 ${THIS_SH} ./array14.sub
400 ${THIS_SH} ./array15.sub
402 ${THIS_SH} ./array16.sub