1 #!/usr/bin/env bash
2 #
3 # Test the length oeprator, which dash supports. Dash doesn't support most
4 # other ops.
5
6 #### String length
7 v=foo
8 echo ${#v}
9 ## stdout: 3
10
11 #### Unicode string length (UTF-8)
12 v=$'_\u03bc_'
13 echo ${#v}
14 ## stdout: 3
15 ## N-I dash stdout: 9
16 ## N-I mksh stdout: 4
17
18 #### Unicode string length (spec/testdata/utf8-chars.txt)
19 v=$(cat spec/testdata/utf8-chars.txt)
20 echo ${#v}
21 ## stdout: 7
22 ## N-I dash stdout: 13
23 ## N-I mksh stdout: 13
24
25 #### String length with incomplete utf-8
26 for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13; do
27 s=$(head -c $num_bytes spec/testdata/utf8-chars.txt)
28 echo ${#s}
29 done
30 ## STDOUT:
31 0
32 1
33 2
34 -1
35 3
36 4
37 -1
38 -1
39 5
40 6
41 -1
42 -1
43 -1
44 7
45 ## END
46 ## STDERR:
47 [??? no location ???] warning: Incomplete UTF-8 character
48 [??? no location ???] warning: Incomplete UTF-8 character
49 [??? no location ???] warning: Incomplete UTF-8 character
50 [??? no location ???] warning: Incomplete UTF-8 character
51 [??? no location ???] warning: Incomplete UTF-8 character
52 [??? no location ???] warning: Incomplete UTF-8 character
53 ## END
54 # zsh behavior actually matches bash!
55 ## BUG bash/zsh stderr-json: ""
56 ## BUG bash/zsh STDOUT:
57 0
58 1
59 2
60 3
61 3
62 4
63 5
64 6
65 5
66 6
67 7
68 8
69 9
70 7
71 ## END
72 ## BUG dash/mksh stderr-json: ""
73 ## N-I dash/mksh STDOUT:
74 0
75 1
76 2
77 3
78 4
79 5
80 6
81 7
82 8
83 9
84 10
85 11
86 12
87 13
88 ## END
89
90 #### String length with invalid utf-8 continuation bytes
91 for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
92 s=$(head -c $num_bytes spec/testdata/utf8-chars.txt)$(echo -e "\xFF")
93 echo ${#s}
94 done
95 ## STDOUT:
96 -1
97 -1
98 -1
99 -1
100 -1
101 -1
102 -1
103 -1
104 -1
105 -1
106 -1
107 -1
108 -1
109 -1
110 -1
111 ## END
112 ## STDERR:
113 [??? no location ???] warning: Invalid start of UTF-8 character
114 [??? no location ???] warning: Invalid start of UTF-8 character
115 [??? no location ???] warning: Invalid start of UTF-8 character
116 [??? no location ???] warning: Invalid UTF-8 continuation byte
117 [??? no location ???] warning: Invalid start of UTF-8 character
118 [??? no location ???] warning: Invalid start of UTF-8 character
119 [??? no location ???] warning: Invalid UTF-8 continuation byte
120 [??? no location ???] warning: Invalid UTF-8 continuation byte
121 [??? no location ???] warning: Invalid start of UTF-8 character
122 [??? no location ???] warning: Invalid start of UTF-8 character
123 [??? no location ???] warning: Invalid UTF-8 continuation byte
124 [??? no location ???] warning: Invalid UTF-8 continuation byte
125 [??? no location ???] warning: Invalid UTF-8 continuation byte
126 [??? no location ???] warning: Invalid start of UTF-8 character
127 [??? no location ???] warning: Invalid start of UTF-8 character
128 ## END
129 ## BUG bash/zsh stderr-json: ""
130 ## BUG bash/zsh STDOUT:
131 1
132 2
133 3
134 4
135 4
136 5
137 6
138 7
139 6
140 7
141 8
142 9
143 10
144 8
145 8
146 ## N-I dash stderr-json: ""
147 ## N-I dash STDOUT:
148 7
149 8
150 9
151 10
152 11
153 12
154 13
155 14
156 15
157 16
158 17
159 18
160 19
161 20
162 20
163 ## END
164 ## N-I mksh stderr-json: ""
165 ## N-I mksh STDOUT:
166 1
167 2
168 3
169 4
170 5
171 6
172 7
173 8
174 9
175 10
176 11
177 12
178 13
179 14
180 14
181 ## END
182
183 #### Length of undefined variable
184 echo ${#undef}
185 ## stdout: 0
186
187 #### Length of undefined variable with nounset
188 set -o nounset
189 echo ${#undef}
190 ## status: 1
191 ## OK dash status: 2
192