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 # make sure declare -a converts an existing variable to an array
27 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
36 # this should print out values, too
37 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
60 a[4+5/2]="test expression"
65 # these two lines should output `declare' commands
66 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
67 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
68 # this line should output `readonly' commands, even for arrays
70 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
73 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
77 declare -a e[10]='(test)'
80 declare -a f='("${d[@]}")'
81 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
85 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
98 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
103 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
112 # the prompt should not print when using a here doc
113 read -p "array test: " -a rv <<!
114 this is a test of read using arrays
117 echo ${rv[0]} ${rv[4]}
120 # the variable should be converted to an array when `read -a' is done
123 this is a test of arrays
125 echo ${vv[0]} ${vv[3]}
129 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
139 echo efgh | ( read x[1] ; echo ${x[1]} )
140 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
142 # Make sure that arrays can be used to save the positional paramters verbatim
143 set -- a 'b c' d 'e f g' h
147 for z in "${ARGV[@]}"
158 # do various pattern removal and length tests
159 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
161 xpath=( $( IFS=: ; echo $XPATH ) )
166 echo ${xpath[@]%%[!/]*}
167 echo ${xpath[0]%%[!/]*}
169 # let's try to make it a DOS-style path
171 zecho "${xpath[@]/\//\\}"
172 zecho "${xpath[@]//\//\\}"
173 zecho "${xpath[@]//[\/]/\\}"
175 # length of the first element of the array, since array without subscript
176 # is equivalent to referencing first element
177 echo ${#xpath} -- ${#xpath[0]}
179 # number of elements in the array
181 echo ${#xpath[@]} -- $nelem
183 # total length of all elements in the array, including space separators
187 # total length of all elements in the array
188 xx=$( IFS='' ; echo "${xpath[*]}" )
194 echo ${#xpath[@]} -- $nelem
196 # arrays and things that look like index assignments
197 array=(42 [1]=14 [2]=44)
199 array2=(grep [ 123 ] \*)
204 # arrays and implicit arithmetic evaluation
207 iarray=( 2+4 1+6 7+2 )
213 # make sure assignment using the compound assignment syntax removes all
214 # of the old elements from the array value
215 barray=(old1 old2 old3 old4 old5)
216 barray=(new1 new2 new3)
217 echo "length = ${#barray[@]}"
218 echo "value = ${barray[*]}"
220 # make sure the array code behaves correctly with respect to unset variables
222 ( echo ${#narray[4]} )