Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 8 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 9 | # first make sure we handle the basics |
| 10 | x=() |
| 11 | echo ${x[@]} |
| 12 | unset x |
| 13 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 14 | # this should be an error |
| 15 | test=(first & second) |
| 16 | echo $? |
| 17 | unset test |
| 18 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 19 | # make sure declare -a converts an existing variable to an array |
| 20 | unset a |
| 21 | a=abcde |
| 22 | declare -a a |
| 23 | echo ${a[0]} |
| 24 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 25 | unset a |
| 26 | a=abcde |
| 27 | a[2]=bdef |
| 28 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 29 | unset b |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 30 | declare -a b[256] |
| 31 | |
| 32 | unset c[2] |
| 33 | unset c[*] |
| 34 | |
| 35 | a[1]= |
| 36 | |
| 37 | _ENV=/bin/true |
| 38 | x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]} |
| 39 | |
| 40 | declare -r c[100] |
| 41 | |
| 42 | echo ${a[0]} ${a[4]} |
| 43 | echo ${a[@]} |
| 44 | |
| 45 | echo ${a[*]} |
| 46 | |
| 47 | # this should print out values, too |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 48 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 49 | |
| 50 | unset a[7] |
| 51 | echo ${a[*]} |
| 52 | |
| 53 | unset a[4] |
| 54 | echo ${a[*]} |
| 55 | |
| 56 | echo ${a} |
| 57 | echo "${a}" |
| 58 | echo $a |
| 59 | |
| 60 | unset a[0] |
| 61 | echo ${a} |
| 62 | |
| 63 | echo ${a[@]} |
| 64 | |
| 65 | a[5]="hello world" |
| 66 | echo ${a[5]} |
| 67 | echo ${#a[5]} |
| 68 | |
| 69 | echo ${#a[@]} |
| 70 | |
| 71 | a[4+5/2]="test expression" |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 72 | declare a["7 + 8"]="test 2" |
| 73 | a[7 + 8]="test 2" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | echo ${a[@]} |
| 75 | |
| 76 | readonly a[5] |
| 77 | readonly a |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 78 | # these two lines should output `declare' commands |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 79 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
| 80 | declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 81 | # this line should output `readonly' commands, even for arrays |
| 82 | set -o posix |
| 83 | readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
| 84 | set +o posix |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 85 | |
| 86 | declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' |
| 87 | d[9]="ninth element" |
| 88 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 89 | declare -a e[10]=test # this works in post-bash-2.05 versions |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 90 | declare -a e[10]='(test)' |
| 91 | |
| 92 | pass=/etc/passwd |
| 93 | declare -a f='("${d[@]}")' |
| 94 | b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass) |
| 95 | |
| 96 | echo ${b[@]:2:3} |
| 97 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 98 | declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | |
| 100 | a[3]="this is a test" |
| 101 | |
| 102 | b[]=bcde |
| 103 | b[*]=aaa |
| 104 | echo ${b[ ]} |
| 105 | |
| 106 | c[-2]=4 |
| 107 | echo ${c[-4]} |
| 108 | |
| 109 | d[7]=(abdedfegeee) |
| 110 | |
| 111 | d=([]=abcde [1]="test test" [*]=last [-65]=negative ) |
| 112 | |
| 113 | unset d[12] |
| 114 | unset e[*] |
| 115 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 116 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | |
| 118 | ps1='hello' |
| 119 | unset ps1[2] |
| 120 | unset ${ps1[2]} |
| 121 | |
| 122 | declare +a ps1 |
| 123 | declare +a c |
| 124 | |
| 125 | # the prompt should not print when using a here doc |
| 126 | read -p "array test: " -a rv <<! |
| 127 | this is a test of read using arrays |
| 128 | ! |
| 129 | |
| 130 | echo ${rv[0]} ${rv[4]} |
| 131 | echo ${rv[@]} |
| 132 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 133 | # the variable should be converted to an array when `read -a' is done |
| 134 | vv=1 |
| 135 | read -a vv <<! |
| 136 | this is a test of arrays |
| 137 | ! |
| 138 | echo ${vv[0]} ${vv[3]} |
| 139 | echo ${vv[@]} |
| 140 | unset vv |
| 141 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 142 | declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)' |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 143 | |
| 144 | export rv |
| 145 | #set |
| 146 | |
| 147 | x[4]=bbb |
| 148 | x=abde |
| 149 | echo $x |
| 150 | echo ${x[0]} |
| 151 | echo ${x[4]} |
| 152 | echo efgh | ( read x[1] ; echo ${x[1]} ) |
| 153 | echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} ) |
| 154 | |
| 155 | # Make sure that arrays can be used to save the positional paramters verbatim |
| 156 | set -- a 'b c' d 'e f g' h |
| 157 | |
| 158 | ARGV=( [0]=$0 "$@" ) |
| 159 | |
| 160 | for z in "${ARGV[@]}" |
| 161 | do |
| 162 | echo "$z" |
| 163 | done |
| 164 | |
| 165 | echo "$0" |
| 166 | for z in "$@" |
| 167 | do |
| 168 | echo "$z" |
| 169 | done |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 170 | |
| 171 | # do various pattern removal and length tests |
| 172 | XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin |
| 173 | |
| 174 | xpath=( $( IFS=: ; echo $XPATH ) ) |
| 175 | |
| 176 | echo ${xpath[@]} |
| 177 | echo ${xpath[@]##*/} |
| 178 | echo ${xpath[0]##*/} |
| 179 | echo ${xpath[@]%%[!/]*} |
| 180 | echo ${xpath[0]%%[!/]*} |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 181 | recho ${xpath##*/} |
| 182 | recho ${xpath%%[!/]*} |
| 183 | recho ${xpath[5]##*/} |
| 184 | recho ${xpath[5]%%[!/]*} |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 185 | |
| 186 | # let's try to make it a DOS-style path |
| 187 | |
| 188 | zecho "${xpath[@]/\//\\}" |
| 189 | zecho "${xpath[@]//\//\\}" |
| 190 | zecho "${xpath[@]//[\/]/\\}" |
| 191 | |
| 192 | # length of the first element of the array, since array without subscript |
| 193 | # is equivalent to referencing first element |
| 194 | echo ${#xpath} -- ${#xpath[0]} |
| 195 | |
| 196 | # number of elements in the array |
| 197 | nelem=${#xpath[@]} |
| 198 | echo ${#xpath[@]} -- $nelem |
| 199 | |
| 200 | # total length of all elements in the array, including space separators |
| 201 | xx="${xpath[*]}" |
| 202 | echo ${#xx} |
| 203 | |
| 204 | # total length of all elements in the array |
| 205 | xx=$( IFS='' ; echo "${xpath[*]}" ) |
| 206 | echo ${#xx} |
| 207 | |
| 208 | unset xpath[nelem-1] |
| 209 | |
| 210 | nelem=${#xpath[@]} |
| 211 | echo ${#xpath[@]} -- $nelem |
| 212 | |
| 213 | # arrays and things that look like index assignments |
| 214 | array=(42 [1]=14 [2]=44) |
| 215 | |
| 216 | array2=(grep [ 123 ] \*) |
| 217 | |
| 218 | echo ${array[@]} |
| 219 | echo "${array2[@]}" |
| 220 | |
| 221 | # arrays and implicit arithmetic evaluation |
| 222 | declare -i -a iarray |
| 223 | |
| 224 | iarray=( 2+4 1+6 7+2 ) |
| 225 | echo ${iarray[@]} |
| 226 | |
| 227 | iarray[4]=4+1 |
| 228 | echo ${iarray[@]} |
| 229 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 230 | # make sure assignment using the compound assignment syntax removes all |
| 231 | # of the old elements from the array value |
| 232 | barray=(old1 old2 old3 old4 old5) |
| 233 | barray=(new1 new2 new3) |
| 234 | echo "length = ${#barray[@]}" |
| 235 | echo "value = ${barray[*]}" |
| 236 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 237 | # make sure the array code behaves correctly with respect to unset variables |
| 238 | set -u |
| 239 | ( echo ${#narray[4]} ) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 240 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 241 | ${THIS_SH} ./array1.sub |
| 242 | ${THIS_SH} ./array2.sub |
| 243 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 244 | # some old bugs and ksh93 compatibility tests |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 245 | ${THIS_SH} ./array3.sub |
| 246 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 247 | # some compound assingment parsing problems that showed up in bash-3.1-release |
| 248 | ${THIS_SH} ./array4.sub |
| 249 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 250 | set +u |
| 251 | cd /tmp |
| 252 | |
| 253 | touch 1=bar |
| 254 | foo=([10]="bar") |
| 255 | echo ${foo[0]} |
| 256 | rm 1=bar |
| 257 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 258 | cd $OLDPWD |
| 259 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 260 | foo=(a b c d e f g) |
| 261 | echo ${foo[@]} |
| 262 | |
| 263 | # quoted reserved words are ok |
| 264 | foo=(\for \case \if \then \else) |
| 265 | echo ${foo[@]} |
| 266 | |
| 267 | # quoted metacharacters are ok |
| 268 | foo=( [1]='<>' [2]='<' [3]='>' [4]='!' ) |
| 269 | echo ${foo[@]} |
| 270 | |
| 271 | # numbers are just words when not in a redirection context |
| 272 | foo=( 12 14 16 18 20 ) |
| 273 | echo ${foo[@]} |
| 274 | |
| 275 | foo=( 4414758999202 ) |
| 276 | echo ${foo[@]} |
| 277 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 278 | # this was a bug in all versions of bash 2.x up to and including bash-2.04 |
| 279 | declare -a ddd=(aaa |
| 280 | bbb) |
| 281 | echo ${ddd[@]} |
| 282 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 283 | # errors until post-bash-2.05a; now reserved words are OK |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 284 | foo=(a b c for case if then else) |
| 285 | |
| 286 | foo=(for case if then else) |
| 287 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 288 | # errors |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 289 | metas=( <> < > ! ) |
| 290 | metas=( [1]=<> [2]=< [3]=> [4]=! ) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 291 | |
| 292 | # various expansions that didn't really work right until post-bash-2.04 |
| 293 | foo='abc' |
| 294 | echo ${foo[0]} ${#foo[0]} |
| 295 | echo ${foo[1]} ${#foo[1]} |
| 296 | echo ${foo[@]} ${#foo[@]} |
| 297 | echo ${foo[*]} ${#foo[*]} |
| 298 | |
| 299 | foo='' |
| 300 | echo ${foo[0]} ${#foo[0]} |
| 301 | echo ${foo[1]} ${#foo[1]} |
| 302 | echo ${foo[@]} ${#foo[@]} |
| 303 | echo ${foo[*]} ${#foo[*]} |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 304 | |
| 305 | # new expansions added after bash-2.05b |
| 306 | x[0]=zero |
| 307 | x[1]=one |
| 308 | x[4]=four |
| 309 | x[10]=ten |
| 310 | |
| 311 | recho ${!x[@]} |
| 312 | recho "${!x[@]}" |
| 313 | recho ${!x[*]} |
| 314 | recho "${!x[*]}" |
| 315 | |
| 316 | # sparse array tests for code fixed in bash-3.0 |
| 317 | unset av |
| 318 | av[1]='one' |
| 319 | av[2]='' |
| 320 | |
| 321 | av[3]=three |
| 322 | av[5]=five |
| 323 | av[7]=seven |
| 324 | |
| 325 | echo include null element -- expect one |
| 326 | echo ${av[@]:1:2} # what happens when we include a null element? |
| 327 | echo include unset element -- expect three five |
| 328 | echo ${av[@]:3:2} # what happens when we include an unset element? |
| 329 | echo start at unset element -- expect five seven |
| 330 | echo ${av[@]:4:2} # what happens when we start at an unset element? |
| 331 | |
| 332 | echo too many elements -- expect three five seven |
| 333 | echo ${av[@]:3:5} # how about too many elements? |
| 334 | |
| 335 | echo positive offset - expect five seven |
| 336 | echo ${av[@]:5:2} |
Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 337 | echo negative offset to unset element - expect seven |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 338 | echo ${av[@]: -2:2} |
| 339 | |
| 340 | echo positive offset 2 - expect seven |
| 341 | echo ${av[@]: 6:2} |
| 342 | echo negative offset 2 - expect seven |
| 343 | echo ${av[@]: -1:2} |
| 344 | |
| 345 | echo out-of-range offset |
| 346 | echo ${av[@]:12} |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 347 | |
| 348 | # parsing problems and other inconsistencies not fixed until post bash-3.0 |
| 349 | unset x |
| 350 | declare -a x=(')' $$) |
| 351 | [ ${x[1]} -eq $$ ] || echo bad |
| 352 | |
| 353 | unset x |
| 354 | declare -a x=(a b c d e) |
| 355 | echo ${x[4]} |
| 356 | |
| 357 | z=([1]=one [4]=four [7]=seven [10]=ten) |
| 358 | |
| 359 | echo ${#z[@]} |
| 360 | |
| 361 | echo ${!z[@]} |
| 362 | |
| 363 | unset x |
| 364 | declare -a x=(a \'b c\') |
| 365 | |
| 366 | echo "${x[1]}" |
| 367 | |
| 368 | unset x |
| 369 | declare -a x=(a 'b c') |
| 370 | |
| 371 | echo "${x[1]}" |
| 372 | |
| 373 | unset x |
| 374 | declare -a x=($0) |
| 375 | [ "${x[@]}" = $0 ] || echo double expansion of \$0 |
| 376 | declare -a x=(\$0) |
| 377 | echo "${x[@]}" |
| 378 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 379 | # tests for bash-3.1 problems |
| 380 | ${THIS_SH} ./array5.sub |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 381 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 382 | # tests for post-bash-3.2 problems, most fixed in bash-3.2 patches |
| 383 | ${THIS_SH} ./array6.sub |
| 384 | ${THIS_SH} ./array7.sub |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 385 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 386 | ${THIS_SH} ./array8.sub |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 387 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 388 | ${THIS_SH} ./array9.sub |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 389 | |
| 390 | ${THIS_SH} ./array10.sub |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 391 | |
| 392 | ${THIS_SH} ./array11.sub |
| 393 | |
| 394 | ${THIS_SH} ./array12.sub |
| 395 | |
| 396 | ${THIS_SH} ./array13.sub |
| 397 | |
| 398 | ${THIS_SH} ./array14.sub |
| 399 | |
| 400 | ${THIS_SH} ./array15.sub |
| 401 | |
| 402 | ${THIS_SH} ./array16.sub |