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 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
66 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
68 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
72 declare -a e[10]='(test)'
75 declare -a f='("${d[@]}")'
76 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
80 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
93 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
98 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
107 # the prompt should not print when using a here doc
108 read -p "array test: " -a rv <<!
109 this is a test of read using arrays
112 echo ${rv[0]} ${rv[4]}
115 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
125 echo efgh | ( read x[1] ; echo ${x[1]} )
126 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
128 # Make sure that arrays can be used to save the positional paramters verbatim
129 set -- a 'b c' d 'e f g' h
133 for z in "${ARGV[@]}"
144 # do various pattern removal and length tests
145 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
147 xpath=( $( IFS=: ; echo $XPATH ) )
152 echo ${xpath[@]%%[!/]*}
153 echo ${xpath[0]%%[!/]*}
155 # let's try to make it a DOS-style path
157 zecho "${xpath[@]/\//\\}"
158 zecho "${xpath[@]//\//\\}"
159 zecho "${xpath[@]//[\/]/\\}"
161 # length of the first element of the array, since array without subscript
162 # is equivalent to referencing first element
163 echo ${#xpath} -- ${#xpath[0]}
165 # number of elements in the array
167 echo ${#xpath[@]} -- $nelem
169 # total length of all elements in the array, including space separators
173 # total length of all elements in the array
174 xx=$( IFS='' ; echo "${xpath[*]}" )
180 echo ${#xpath[@]} -- $nelem
182 # arrays and things that look like index assignments
183 array=(42 [1]=14 [2]=44)
185 array2=(grep [ 123 ] \*)
190 # arrays and implicit arithmetic evaluation
193 iarray=( 2+4 1+6 7+2 )
199 # make sure the array code behaves correctly with respect to unset variables
201 ( echo ${#narray[4]} )