6e56d5a9f7e765cc14d5c44d24e7cc7b3a0d8d68
[platform/upstream/bash.git] / tests / array.tests
1 # this is needed so that the bad assignments (b[]=bcde, for example) do not
2 # cause fatal shell errors when in posix mode
3 set +o posix
4
5 set +a
6 # The calls to egrep -v are to filter out builtin array variables that are
7 # automatically set and possibly contain values that vary.
8
9 # make sure declare -a converts an existing variable to an array
10 unset a
11 a=abcde
12 declare -a a
13 echo ${a[0]}
14
15 unset a
16 a=abcde
17 a[2]=bdef
18
19 declare -a b[256]
20
21 unset c[2]
22 unset c[*]
23
24 a[1]=
25
26 _ENV=/bin/true
27 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
28
29 declare -r c[100]
30
31 echo ${a[0]} ${a[4]}
32 echo ${a[@]}
33
34 echo ${a[*]}
35
36 # this should print out values, too
37 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
38
39 unset a[7]
40 echo ${a[*]}
41
42 unset a[4]
43 echo ${a[*]}
44
45 echo ${a}
46 echo "${a}"
47 echo $a
48
49 unset a[0]
50 echo ${a}
51
52 echo ${a[@]}
53
54 a[5]="hello world"
55 echo ${a[5]}
56 echo ${#a[5]}
57
58 echo ${#a[@]}
59
60 a[4+5/2]="test expression"
61 echo ${a[@]}
62
63 readonly a[5]
64 readonly a
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
69 set -o posix
70 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
71 set +o posix
72
73 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
74 d[9]="ninth element"
75
76 declare -a e[10]=test
77 declare -a e[10]='(test)'
78
79 pass=/etc/passwd
80 declare -a f='("${d[@]}")'
81 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
82
83 echo ${b[@]:2:3}
84
85 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
86
87 a[3]="this is a test"
88
89 b[]=bcde
90 b[*]=aaa
91 echo ${b[   ]}
92
93 c[-2]=4
94 echo ${c[-4]}
95
96 d[7]=(abdedfegeee)
97
98 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
99
100 unset d[12]
101 unset e[*]
102
103 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
104
105 ps1='hello'
106 unset ps1[2]
107 unset ${ps1[2]}
108
109 declare +a ps1
110 declare +a c
111
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
115 !
116
117 echo ${rv[0]} ${rv[4]}
118 echo ${rv[@]}
119
120 # the variable should be converted to an array when `read -a' is done
121 vv=1
122 read -a vv <<!
123 this is a test of arrays
124 !
125 echo ${vv[0]} ${vv[3]}
126 echo ${vv[@]}
127 unset vv
128
129 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
130
131 export rv
132 #set
133
134 x[4]=bbb
135 x=abde
136 echo $x
137 echo ${x[0]}
138 echo ${x[4]}
139 echo efgh | ( read x[1] ; echo ${x[1]} )
140 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
141
142 # Make sure that arrays can be used to save the positional paramters verbatim
143 set -- a 'b c' d 'e f g' h
144
145 ARGV=( [0]=$0 "$@" )
146
147 for z in "${ARGV[@]}"
148 do
149         echo "$z"
150 done
151
152 echo "$0"
153 for z in "$@"
154 do
155         echo "$z"
156 done
157
158 # do various pattern removal and length tests
159 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
160
161 xpath=( $( IFS=: ; echo $XPATH ) )
162
163 echo ${xpath[@]}
164 echo ${xpath[@]##*/}
165 echo ${xpath[0]##*/}
166 echo ${xpath[@]%%[!/]*}
167 echo ${xpath[0]%%[!/]*}
168
169 # let's try to make it a DOS-style path
170
171 zecho "${xpath[@]/\//\\}"
172 zecho "${xpath[@]//\//\\}"
173 zecho "${xpath[@]//[\/]/\\}"
174
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]}
178
179 # number of elements in the array
180 nelem=${#xpath[@]}
181 echo ${#xpath[@]} -- $nelem
182
183 # total length of all elements in the array, including space separators
184 xx="${xpath[*]}"
185 echo ${#xx}
186
187 # total length of all elements in the array
188 xx=$( IFS='' ; echo "${xpath[*]}" )
189 echo ${#xx}
190
191 unset xpath[nelem-1]
192
193 nelem=${#xpath[@]}
194 echo ${#xpath[@]} -- $nelem
195
196 # arrays and things that look like index assignments
197 array=(42 [1]=14 [2]=44)
198
199 array2=(grep [ 123 ] \*)
200
201 echo ${array[@]}
202 echo "${array2[@]}"
203
204 # arrays and implicit arithmetic evaluation
205 declare -i -a iarray
206
207 iarray=( 2+4 1+6 7+2 )
208 echo ${iarray[@]}
209
210 iarray[4]=4+1
211 echo ${iarray[@]}
212
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[*]}"
219
220 # make sure the array code behaves correctly with respect to unset variables
221 set -u
222 ( echo ${#narray[4]} )