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 # this should be an error
14 # make sure declare -a converts an existing variable to an array
32 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
41 # this should print out values, too
42 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
65 a[4+5/2]="test expression"
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
75 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
78 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
82 declare -a e[10]='(test)'
85 declare -a f='("${d[@]}")'
86 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
90 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
103 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
108 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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
122 echo ${rv[0]} ${rv[4]}
125 # the variable should be converted to an array when `read -a' is done
128 this is a test of arrays
130 echo ${vv[0]} ${vv[3]}
134 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
144 echo efgh | ( read x[1] ; echo ${x[1]} )
145 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
147 # Make sure that arrays can be used to save the positional paramters verbatim
148 set -- a 'b c' d 'e f g' h
152 for z in "${ARGV[@]}"
163 # do various pattern removal and length tests
164 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
166 xpath=( $( IFS=: ; echo $XPATH ) )
171 echo ${xpath[@]%%[!/]*}
172 echo ${xpath[0]%%[!/]*}
174 # let's try to make it a DOS-style path
176 zecho "${xpath[@]/\//\\}"
177 zecho "${xpath[@]//\//\\}"
178 zecho "${xpath[@]//[\/]/\\}"
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]}
184 # number of elements in the array
186 echo ${#xpath[@]} -- $nelem
188 # total length of all elements in the array, including space separators
192 # total length of all elements in the array
193 xx=$( IFS='' ; echo "${xpath[*]}" )
199 echo ${#xpath[@]} -- $nelem
201 # arrays and things that look like index assignments
202 array=(42 [1]=14 [2]=44)
204 array2=(grep [ 123 ] \*)
209 # arrays and implicit arithmetic evaluation
212 iarray=( 2+4 1+6 7+2 )
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[*]}"
225 # make sure the array code behaves correctly with respect to unset variables
227 ( echo ${#narray[4]} )
229 # some old bugs and ksh93 compatibility tests
241 # quoted reserved words are ok
242 foo=(\for \case \if \then \else)
245 # quoted metacharacters are ok
246 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
249 # numbers are just words when not in a redirection context
250 foo=( 12 14 16 18 20 )
253 foo=( 4414758999202 )
256 # this was a bug in all versions of bash 2.x up to and including bash-2.04
262 foo=(a b c for case if then else)
264 foo=(for case if then else)
267 metas=( [1]=<> [2]=< [3]=> [4]=! )
269 # various expansions that didn't really work right until post-bash-2.04
271 echo ${foo[0]} ${#foo[0]}
272 echo ${foo[1]} ${#foo[1]}
273 echo ${foo[@]} ${#foo[@]}
274 echo ${foo[*]} ${#foo[*]}
277 echo ${foo[0]} ${#foo[0]}
278 echo ${foo[1]} ${#foo[1]}
279 echo ${foo[@]} ${#foo[@]}
280 echo ${foo[*]} ${#foo[*]}