2ee376bbbf150974a8e5f60dc6c7d0e0c3f585b3
[platform/upstream/bash.git] / tests / array.tests
1 set +a
2 # The calls to egrep -v are to filter out builtin array variables that are
3 # automatically set and possibly contain values that vary.
4 unset a
5 a=abcde
6 a[2]=bdef
7
8 declare -a b[256]
9
10 unset c[2]
11 unset c[*]
12
13 a[1]=
14
15 _ENV=/bin/true
16 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
17
18 declare -r c[100]
19
20 echo ${a[0]} ${a[4]}
21 echo ${a[@]}
22
23 echo ${a[*]}
24
25 # this should print out values, too
26 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
27
28 unset a[7]
29 echo ${a[*]}
30
31 unset a[4]
32 echo ${a[*]}
33
34 echo ${a}
35 echo "${a}"
36 echo $a
37
38 unset a[0]
39 echo ${a}
40
41 echo ${a[@]}
42
43 a[5]="hello world"
44 echo ${a[5]}
45 echo ${#a[5]}
46
47 echo ${#a[@]}
48
49 a[4+5/2]="test expression"
50 echo ${a[@]}
51
52 readonly a[5]
53 readonly a
54 readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
55 declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
56
57 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
58 d[9]="ninth element"
59
60 declare -a e[10]=test
61 declare -a e[10]='(test)'
62
63 pass=/etc/passwd
64 declare -a f='("${d[@]}")'
65 b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
66
67 echo ${b[@]:2:3}
68
69 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
70
71 a[3]="this is a test"
72
73 b[]=bcde
74 b[*]=aaa
75 echo ${b[   ]}
76
77 c[-2]=4
78 echo ${c[-4]}
79
80 d[7]=(abdedfegeee)
81
82 d=([]=abcde [1]="test test" [*]=last [-65]=negative )
83
84 unset d[12]
85 unset e[*]
86
87 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
88
89 ps1='hello'
90 unset ps1[2]
91 unset ${ps1[2]}
92
93 declare +a ps1
94 declare +a c
95
96 # the prompt should not print when using a here doc
97 read -p "array test: " -a rv <<!
98 this is a test of read using arrays
99 !
100
101 echo ${rv[0]} ${rv[4]}
102 echo ${rv[@]}
103
104 declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS)'
105
106 export rv
107 #set
108
109 x[4]=bbb
110 x=abde
111 echo $x
112 echo ${x[0]}
113 echo ${x[4]}
114 echo efgh | ( read x[1] ; echo ${x[1]} )
115 echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
116
117 # Make sure that arrays can be used to save the positional paramters verbatim
118 set -- a 'b c' d 'e f g' h
119
120 ARGV=( [0]=$0 "$@" )
121
122 for z in "${ARGV[@]}"
123 do
124         echo "$z"
125 done
126
127 echo "$0"
128 for z in "$@"
129 do
130         echo "$z"
131 done