Imported from ../bash-2.01.tar.gz.
[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 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
66 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
67
68 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
69 d[9]="ninth element"
70
71 declare -a e[10]=test
72 declare -a e[10]='(test)'
73
74 pass=/etc/passwd
75 declare -a f='("${d[@]}")'
76 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
77
78 echo ${b[@]:2:3}
79
80 declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
81
82 a[3]="this is a test"
83
84 b[]=bcde
85 b[*]=aaa
86 echo ${b[   ]}
87
88 c[-2]=4
89 echo ${c[-4]}
90
91 d[7]=(abdedfegeee)
92
93 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
94
95 unset d[12]
96 unset e[*]
97
98 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
99
100 ps1='hello'
101 unset ps1[2]
102 unset ${ps1[2]}
103
104 declare +a ps1
105 declare +a c
106
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
110 !
111
112 echo ${rv[0]} ${rv[4]}
113 echo ${rv[@]}
114
115 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
116
117 export rv
118 #set
119
120 x[4]=bbb
121 x=abde
122 echo $x
123 echo ${x[0]}
124 echo ${x[4]}
125 echo efgh | ( read x[1] ; echo ${x[1]} )
126 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
127
128 # Make sure that arrays can be used to save the positional paramters verbatim
129 set -- a 'b c' d 'e f g' h
130
131 ARGV=( [0]=$0 "$@" )
132
133 for z in "${ARGV[@]}"
134 do
135         echo "$z"
136 done
137
138 echo "$0"
139 for z in "$@"
140 do
141         echo "$z"
142 done
143
144 # do various pattern removal and length tests
145 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
146
147 xpath=( $( IFS=: ; echo $XPATH ) )
148
149 echo ${xpath[@]}
150 echo ${xpath[@]##*/}
151 echo ${xpath[0]##*/}
152 echo ${xpath[@]%%[!/]*}
153 echo ${xpath[0]%%[!/]*}
154
155 # let's try to make it a DOS-style path
156
157 zecho "${xpath[@]/\//\\}"
158 zecho "${xpath[@]//\//\\}"
159 zecho "${xpath[@]//[\/]/\\}"
160
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]}
164
165 # number of elements in the array
166 nelem=${#xpath[@]}
167 echo ${#xpath[@]} -- $nelem
168
169 # total length of all elements in the array, including space separators
170 xx="${xpath[*]}"
171 echo ${#xx}
172
173 # total length of all elements in the array
174 xx=$( IFS='' ; echo "${xpath[*]}" )
175 echo ${#xx}
176
177 unset xpath[nelem-1]
178
179 nelem=${#xpath[@]}
180 echo ${#xpath[@]} -- $nelem
181
182 # arrays and things that look like index assignments
183 array=(42 [1]=14 [2]=44)
184
185 array2=(grep [ 123 ] \*)
186
187 echo ${array[@]}
188 echo "${array2[@]}"
189
190 # arrays and implicit arithmetic evaluation
191 declare -i -a iarray
192
193 iarray=( 2+4 1+6 7+2 )
194 echo ${iarray[@]}
195
196 iarray[4]=4+1
197 echo ${iarray[@]}
198
199 # make sure the array code behaves correctly with respect to unset variables
200 set -u
201 ( echo ${#narray[4]} )