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 # some old bugs and ksh93 compatibility tests
251 # quoted reserved words are ok
252 foo=(\for \case \if \then \else)
255 # quoted metacharacters are ok
256 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
259 # numbers are just words when not in a redirection context
260 foo=( 12 14 16 18 20 )
263 foo=( 4414758999202 )
266 # this was a bug in all versions of bash 2.x up to and including bash-2.04
271 # errors until post-bash-2.05a; now reserved words are OK
272 foo=(a b c for case if then else)
274 foo=(for case if then else)
278 metas=( [1]=<> [2]=< [3]=> [4]=! )
280 # various expansions that didn't really work right until post-bash-2.04
282 echo ${foo[0]} ${#foo[0]}
283 echo ${foo[1]} ${#foo[1]}
284 echo ${foo[@]} ${#foo[@]}
285 echo ${foo[*]} ${#foo[*]}
288 echo ${foo[0]} ${#foo[0]}
289 echo ${foo[1]} ${#foo[1]}
290 echo ${foo[@]} ${#foo[@]}
291 echo ${foo[*]} ${#foo[*]}
293 # new expansions added after bash-2.05b
304 # sparse array tests for code fixed in bash-3.0
313 echo include null element -- expect one
314 echo ${av[@]:1:2} # what happens when we include a null element?
315 echo include unset element -- expect three five
316 echo ${av[@]:3:2} # what happens when we include an unset element?
317 echo start at unset element -- expect five seven
318 echo ${av[@]:4:2} # what happens when we start at an unset element?
320 echo too many elements -- expect three five seven
321 echo ${av[@]:3:5} # how about too many elements?
323 echo positive offset - expect five seven
325 echo negative offset - expect five seven
328 echo positive offset 2 - expect seven
330 echo negative offset 2 - expect seven
333 echo out-of-range offset