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"
76 # these two lines should output `declare' commands
77 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
78 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
79 # this line should output `readonly' commands, even for arrays
81 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
84 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
87 declare -a e[10]=test # this works in post-bash-2.05 versions
88 declare -a e[10]='(test)'
91 declare -a f='("${d[@]}")'
92 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
96 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
109 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
114 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
123 # the prompt should not print when using a here doc
124 read -p "array test: " -a rv <<!
125 this is a test of read using arrays
128 echo ${rv[0]} ${rv[4]}
131 # the variable should be converted to an array when `read -a' is done
134 this is a test of arrays
136 echo ${vv[0]} ${vv[3]}
140 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
150 echo efgh | ( read x[1] ; echo ${x[1]} )
151 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
153 # Make sure that arrays can be used to save the positional paramters verbatim
154 set -- a 'b c' d 'e f g' h
158 for z in "${ARGV[@]}"
169 # do various pattern removal and length tests
170 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
172 xpath=( $( IFS=: ; echo $XPATH ) )
177 echo ${xpath[@]%%[!/]*}
178 echo ${xpath[0]%%[!/]*}
180 recho ${xpath%%[!/]*}
181 recho ${xpath[5]##*/}
182 recho ${xpath[5]%%[!/]*}
184 # let's try to make it a DOS-style path
186 zecho "${xpath[@]/\//\\}"
187 zecho "${xpath[@]//\//\\}"
188 zecho "${xpath[@]//[\/]/\\}"
190 # length of the first element of the array, since array without subscript
191 # is equivalent to referencing first element
192 echo ${#xpath} -- ${#xpath[0]}
194 # number of elements in the array
196 echo ${#xpath[@]} -- $nelem
198 # total length of all elements in the array, including space separators
202 # total length of all elements in the array
203 xx=$( IFS='' ; echo "${xpath[*]}" )
209 echo ${#xpath[@]} -- $nelem
211 # arrays and things that look like index assignments
212 array=(42 [1]=14 [2]=44)
214 array2=(grep [ 123 ] \*)
219 # arrays and implicit arithmetic evaluation
222 iarray=( 2+4 1+6 7+2 )
228 # make sure assignment using the compound assignment syntax removes all
229 # of the old elements from the array value
230 barray=(old1 old2 old3 old4 old5)
231 barray=(new1 new2 new3)
232 echo "length = ${#barray[@]}"
233 echo "value = ${barray[*]}"
235 # make sure the array code behaves correctly with respect to unset variables
237 ( echo ${#narray[4]} )
239 ${THIS_SH} ./array1.sub
240 ${THIS_SH} ./array2.sub
242 # some old bugs and ksh93 compatibility tests
243 ${THIS_SH} ./array3.sub
256 # quoted reserved words are ok
257 foo=(\for \case \if \then \else)
260 # quoted metacharacters are ok
261 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
264 # numbers are just words when not in a redirection context
265 foo=( 12 14 16 18 20 )
268 foo=( 4414758999202 )
271 # this was a bug in all versions of bash 2.x up to and including bash-2.04
276 # errors until post-bash-2.05a; now reserved words are OK
277 foo=(a b c for case if then else)
279 foo=(for case if then else)
283 metas=( [1]=<> [2]=< [3]=> [4]=! )
285 # various expansions that didn't really work right until post-bash-2.04
287 echo ${foo[0]} ${#foo[0]}
288 echo ${foo[1]} ${#foo[1]}
289 echo ${foo[@]} ${#foo[@]}
290 echo ${foo[*]} ${#foo[*]}
293 echo ${foo[0]} ${#foo[0]}
294 echo ${foo[1]} ${#foo[1]}
295 echo ${foo[@]} ${#foo[@]}
296 echo ${foo[*]} ${#foo[*]}
298 # new expansions added after bash-2.05b
309 # sparse array tests for code fixed in bash-3.0
318 echo include null element -- expect one
319 echo ${av[@]:1:2} # what happens when we include a null element?
320 echo include unset element -- expect three five
321 echo ${av[@]:3:2} # what happens when we include an unset element?
322 echo start at unset element -- expect five seven
323 echo ${av[@]:4:2} # what happens when we start at an unset element?
325 echo too many elements -- expect three five seven
326 echo ${av[@]:3:5} # how about too many elements?
328 echo positive offset - expect five seven
330 echo negative offset to unset element - expect seven
333 echo positive offset 2 - expect seven
335 echo negative offset 2 - expect seven
338 echo out-of-range offset
341 # parsing problems and other inconsistencies not fixed until post bash-3.0
343 declare -a x=(')' $$)
344 [ ${x[1]} -eq $$ ] || echo bad
347 declare -a x=(a b c d e)
350 z=([1]=one [4]=four [7]=seven [10]=ten)
357 declare -a x=(a \'b c\')
362 declare -a x=(a 'b c')
368 [ "${x[@]}" = $0 ] || echo double expansion of \$0
374 mkdir $TMPDIR/bash-test-$$
375 cd $TMPDIR/bash-test-$$
377 trap "cd / ; rm -rf $TMPDIR/bash-test-$$" 0 1 2 3 6 15