]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/builtins5.sub
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / builtins5.sub
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
a0c0a00f
CR
14# a start at a test suite for what it means for an array to be set or unset and
15# how to test that state
16typeset -A A
17A[a]=1
18typeset -a a
19a[1]=1
20
21if [ -v A ]; then echo assoc 1; else echo assoc 1 unset; fi
22if [ -v a ]; then echo array 1; else echo array 1 unset; fi
23
24if [ -v "${A[@]}" ]; then echo assoc 2; else echo assoc 2 unset; fi
25if [ -v "${a[@]}" ]; then echo array 2; else echo array 2 unset; fi
26
27echo ${A-unset1}
28echo ${a-unset2}
29
30echo ${A[@]-unset3}
31echo ${a[@]-unset4}
32
33echo ${#A[@]}
34echo ${#a[@]}
35
36typeset -A B
37typeset -a b
38
4491c030
CR
39echo ${#B[@]}
40echo ${#b[@]}
41
a0c0a00f
CR
42scalar1=foo
43scalar2=
44
4491c030 45# this now checks for A[@] treating @ as a valid key - post-bash-5.1
a0c0a00f
CR
46if [ -v A[@] ]; then echo assoc A; else echo assoc A unset; fi
47if [ -v a[@] ]; then echo array a; else echo array a unset; fi
48
4491c030
CR
49if [ ${#A[@]} -gt 0 ]; then echo assoc A; else echo assoc A unset; fi
50if [ ${#a[@]} -gt 0 ]; then echo array a; else echo array a unset; fi
51
a0c0a00f
CR
52if [ -v B[@] ]; then echo assoc B; else echo assoc B unset; fi
53if [ -v b[@] ]; then echo array b; else echo array b unset; fi
54
4491c030
CR
55if [ ${#B[@]} -gt 0 ]; then echo assoc B; else echo assoc B unset; fi
56if [ ${#b[@]} -gt 0 ]; then echo array b; else echo array b unset; fi
57
a0c0a00f
CR
58if [ -v scalar1[@] ]; then echo scalar 1; else echo scalar 1 unset; fi
59if [ -v scalar2[@] ]; then echo scalar 2; else echo scalar 2 unset; fi
60if [ -v scalar3[@] ]; then echo scalar 3; else echo scalar 3 unset; fi
61
62unset a A
63declare -A assoc=([one]=one [two]=two [three]=three)
64declare -a array=(one two three)
65
66scalar="one two three"
67scalar2=
68
69recho "${scalar[@]}"
70
71echo assoc: ${#assoc[@]}
72echo array: ${#array[@]}
73
74echo scalar: ${#scalar}
75echo scalar: ${#scalar[@]}
76
77echo scalar: ${#scalar2}
78echo scalar: ${#scalar2[@]}
79
80echo scalar: ${#scalar3}
81echo scalar: ${#scalar3[@]}
82
83