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
33 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
42 # this should print out values, too
43 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
66 a[4+5/2]="test expression"
71 # these two lines should output `declare' commands
72 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
73 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
74 # this line should output `readonly' commands, even for arrays
76 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
79 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
82 declare -a e[10]=test # this works in post-bash-2.05 versions
83 declare -a e[10]='(test)'
86 declare -a f='("${d[@]}")'
87 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
91 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
104 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
109 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
118 # the prompt should not print when using a here doc
119 read -p "array test: " -a rv <<!
120 this is a test of read using arrays
123 echo ${rv[0]} ${rv[4]}
126 # the variable should be converted to an array when `read -a' is done
129 this is a test of arrays
131 echo ${vv[0]} ${vv[3]}
135 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
145 echo efgh | ( read x[1] ; echo ${x[1]} )
146 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
148 # Make sure that arrays can be used to save the positional paramters verbatim
149 set -- a 'b c' d 'e f g' h
153 for z in "${ARGV[@]}"
164 # do various pattern removal and length tests
165 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
167 xpath=( $( IFS=: ; echo $XPATH ) )
172 echo ${xpath[@]%%[!/]*}
173 echo ${xpath[0]%%[!/]*}
175 # let's try to make it a DOS-style path
177 zecho "${xpath[@]/\//\\}"
178 zecho "${xpath[@]//\//\\}"
179 zecho "${xpath[@]//[\/]/\\}"
181 # length of the first element of the array, since array without subscript
182 # is equivalent to referencing first element
183 echo ${#xpath} -- ${#xpath[0]}
185 # number of elements in the array
187 echo ${#xpath[@]} -- $nelem
189 # total length of all elements in the array, including space separators
193 # total length of all elements in the array
194 xx=$( IFS='' ; echo "${xpath[*]}" )
200 echo ${#xpath[@]} -- $nelem
202 # arrays and things that look like index assignments
203 array=(42 [1]=14 [2]=44)
205 array2=(grep [ 123 ] \*)
210 # arrays and implicit arithmetic evaluation
213 iarray=( 2+4 1+6 7+2 )
219 # make sure assignment using the compound assignment syntax removes all
220 # of the old elements from the array value
221 barray=(old1 old2 old3 old4 old5)
222 barray=(new1 new2 new3)
223 echo "length = ${#barray[@]}"
224 echo "value = ${barray[*]}"
226 # make sure the array code behaves correctly with respect to unset variables
228 ( echo ${#narray[4]} )
230 # some old bugs and ksh93 compatibility tests
242 # quoted reserved words are ok
243 foo=(\for \case \if \then \else)
246 # quoted metacharacters are ok
247 foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
250 # numbers are just words when not in a redirection context
251 foo=( 12 14 16 18 20 )
254 foo=( 4414758999202 )
257 # this was a bug in all versions of bash 2.x up to and including bash-2.04
263 foo=(a b c for case if then else)
265 foo=(for case if then else)
268 metas=( [1]=<> [2]=< [3]=> [4]=! )
270 # various expansions that didn't really work right until post-bash-2.04
272 echo ${foo[0]} ${#foo[0]}
273 echo ${foo[1]} ${#foo[1]}
274 echo ${foo[@]} ${#foo[@]}
275 echo ${foo[*]} ${#foo[*]}
278 echo ${foo[0]} ${#foo[0]}
279 echo ${foo[1]} ${#foo[1]}
280 echo ${foo[@]} ${#foo[@]}
281 echo ${foo[*]} ${#foo[*]}