1 # xtrace test. Test PS4 and line numbers, etc.
2
3 ## oils_failures_allowed: 1
4 ## compare_shells: bash dash mksh
5
6 #### unset PS4
7 case $SH in dash) echo 'weird bug'; exit ;; esac
8
9 set -x
10 echo 1
11 unset PS4
12 echo 2
13 ## STDOUT:
14 1
15 2
16 ## STDERR:
17 + echo 1
18 + unset PS4
19 echo 2
20 ## END
21
22 ## BUG dash STDOUT:
23 weird bug
24 ## END
25 ## BUG dash STDERR:
26 ## END
27
28 #### set -o verbose prints unevaluated code
29 set -o verbose
30 x=foo
31 y=bar
32 echo $x
33 echo $(echo $y)
34 ## STDOUT:
35 foo
36 bar
37 ## STDERR:
38 x=foo
39 y=bar
40 echo $x
41 echo $(echo $y)
42 ## OK bash STDERR:
43 x=foo
44 y=bar
45 echo $x
46 echo $(echo $y)
47 ## END
48
49 #### xtrace with unprintable chars
50 case $SH in (dash) exit ;; esac
51
52 s=$'a\x03b\004c\x00d'
53 set -o xtrace
54 echo "$s"
55 ## stdout-repr: 'a\x03b\x04c\x00d\n'
56 ## STDERR:
57 + echo $'a\u0003b\u0004c\u0000d'
58 ## END
59 ## OK bash stdout-repr: 'a\x03b\x04c\n'
60 ## OK bash stderr-repr: "+ echo $'a\\003b\\004c'\n"
61
62 # nonsensical output?
63 ## BUG mksh stdout-repr: 'a;\x04c\r\n'
64 ## BUG mksh stderr-repr: "+ echo $'a;\\004c\\r'\n"
65 ## N-I dash stdout-json: ""
66 ## N-I dash stderr-json: ""
67
68 #### xtrace with unicode chars
69 case $SH in (dash) exit ;; esac
70
71 mu1='[μ]'
72 mu2=$'[\u03bc]'
73
74 set -o xtrace
75 echo "$mu1" "$mu2"
76
77 ## STDOUT:
78 [μ] [μ]
79 ## END
80 ## STDERR:
81 + echo '[μ]' '[μ]'
82 ## END
83 ## N-I dash stdout-json: ""
84 ## N-I dash stderr-json: ""
85
86 #### xtrace with paths
87 set -o xtrace
88 echo my-dir/my_file.cc
89 ## STDOUT:
90 my-dir/my_file.cc
91 ## END
92 ## STDERR:
93 + echo my-dir/my_file.cc
94 ## END
95
96 #### xtrace with tabs
97 case $SH in (dash) exit ;; esac
98
99 set -o xtrace
100 echo $'[\t]'
101 ## stdout-json: "[\t]\n"
102 ## STDERR:
103 + echo $'[\t]'
104 ## END
105 # this is a bug because it's hard to see
106 ## BUG bash stderr-json: "+ echo '[\t]'\n"
107 ## N-I dash stdout-json: ""
108 ## N-I dash stderr-json: ""
109
110 #### xtrace with whitespace, quotes, and backslash
111 set -o xtrace
112 echo '1 2' \' \" \\
113 ## STDOUT:
114 1 2 ' " \
115 ## END
116
117 # YSH is different because backslashes require $'\\' and not '\', but that's OK
118 ## STDERR:
119 + echo '1 2' $'\'' '"' $'\\'
120 ## END
121
122 ## OK bash/mksh STDERR:
123 + echo '1 2' \' '"' '\'
124 ## END
125
126 ## BUG dash STDERR:
127 + echo 1 2 ' " \
128 ## END
129
130 #### xtrace with newlines
131 # bash and dash trace this badly. They print literal newlines, which I don't
132 # want.
133 set -x
134 echo $'[\n]'
135 ## STDOUT:
136 [
137 ]
138 ## STDERR:
139 + echo $'[\n]'
140 ## END
141 # bash has ugly output that spans lines
142 ## OK bash STDERR:
143 + echo '[
144 ]'
145 ## END
146 ## N-I dash stdout-json: "$[\n]\n"
147 ## N-I dash stderr-json: "+ echo $[\\n]\n"
148
149 #### xtrace written before command executes
150 set -x
151 echo one >&2
152 echo two >&2
153 ## stdout-json: ""
154 ## STDERR:
155 + echo one
156 one
157 + echo two
158 two
159 ## OK mksh STDERR:
160 # mksh traces redirects!
161 + >&2
162 + echo one
163 one
164 + >&2
165 + echo two
166 two
167 ## END
168
169 #### Assignments and assign builtins
170 set -x
171 x=1 x=2; echo $x; readonly x=3
172 ## STDOUT:
173 2
174 ## END
175 ## STDERR:
176 + x=1
177 + x=2
178 + echo 2
179 + readonly x=3
180 ## END
181 ## OK dash STDERR:
182 + x=1 x=2
183 + echo 2
184 + readonly x=3
185 ## END
186 ## OK bash STDERR:
187 + x=1
188 + x=2
189 + echo 2
190 + readonly x=3
191 + x=3
192 ## END
193 ## OK mksh STDERR:
194 + x=1 x=2
195 + echo 2
196 + readonly 'x=3'
197 ## END
198
199 #### [[ ]]
200 case $SH in (dash|mksh) exit ;; esac
201
202 set -x
203
204 dir=/
205 if [[ -d $dir ]]; then
206 (( a = 42 ))
207 fi
208 ## stdout-json: ""
209 ## STDERR:
210 + dir=/
211 + [[ -d $dir ]]
212 + (( a = 42 ))
213 ## END
214 ## OK bash STDERR:
215 + dir=/
216 + [[ -d / ]]
217 + (( a = 42 ))
218 ## END
219 ## N-I dash/mksh stderr-json: ""
220
221 #### PS4 is scoped
222 set -x
223 echo one
224 f() {
225 local PS4='- '
226 echo func;
227 }
228 f
229 echo two
230 ## STDERR:
231 + echo one
232 + f
233 + local 'PS4=- '
234 - echo func
235 + echo two
236 ## END
237 ## OK osh STDERR:
238 + echo one
239 + f
240 + local PS4='- '
241 - echo func
242 + echo two
243 ## END
244 ## OK dash STDERR:
245 # dash loses information about spaces! There is a trailing space, but you
246 # can't see it.
247 + echo one
248 + f
249 + local PS4=-
250 - echo func
251 + echo two
252 ## END
253 ## OK mksh STDERR:
254 # local gets turned into typeset
255 + echo one
256 + f
257 + typeset 'PS4=- '
258 - echo func
259 + echo two
260 ## END
261
262 #### xtrace with variables in PS4
263 PS4='+$x:'
264 set -o xtrace
265 x=1
266 echo one
267 x=2
268 echo two
269 ## STDOUT:
270 one
271 two
272 ## END
273
274 ## STDERR:
275 +:x=1
276 +1:echo one
277 +1:x=2
278 +2:echo two
279 ## END
280
281 ## OK mksh STDERR:
282 # mksh has trailing spaces
283 +:x=1
284 +1:echo one
285 +1:x=2
286 +2:echo two
287 ## END
288
289 ## OK osh/dash STDERR:
290 # the PS4 string is evaluated AFTER the variable is set. That's OK
291 +1:x=1
292 +1:echo one
293 +2:x=2
294 +2:echo two
295 ## END
296
297 #### PS4 with unterminated ${
298 # osh shows inline error; maybe fail like dash/mksh?
299 x=1
300 PS4='+${x'
301 set -o xtrace
302 echo one
303 echo status=$?
304 ## STDOUT:
305 one
306 status=0
307 ## END
308 # mksh and dash both fail. bash prints errors to stderr.
309 ## OK dash stdout-json: ""
310 ## OK dash status: 2
311 ## OK mksh stdout-json: ""
312 ## OK mksh status: 1
313
314 #### PS4 with unterminated $(
315 # osh shows inline error; maybe fail like dash/mksh?
316 x=1
317 PS4='+$(x'
318 set -o xtrace
319 echo one
320 echo status=$?
321 ## STDOUT:
322 one
323 status=0
324 ## END
325 # mksh and dash both fail. bash prints errors to stderr.
326 ## OK dash stdout-json: ""
327 ## OK dash status: 2
328 ## OK mksh stdout-json: ""
329 ## OK mksh status: 1
330
331 #### PS4 with runtime error
332 # osh shows inline error; maybe fail like dash/mksh?
333 x=1
334 PS4='+oops $(( 1 / 0 )) \$'
335 set -o xtrace
336 echo one
337 echo status=$?
338 ## STDOUT:
339 one
340 status=0
341 ## END
342 # mksh and dash both fail. bash prints errors to stderr.
343 ## OK dash stdout-json: ""
344 ## OK dash status: 2
345 ## OK mksh stdout-json: ""
346 ## OK mksh status: 1
347
348
349 #### Reading $? in PS4
350 PS4='[last=$?] '
351 set -x
352 false
353 echo ok
354 ## STDOUT:
355 ok
356 ## END
357 ## STDERR:
358 [last=0] false
359 [last=1] echo ok
360 ## END
361 ## OK osh STDERR:
362 [last=0] 'false'
363 [last=1] echo ok
364 ## END
365
366
367 #### Regression: xtrace for "declare -a a+=(v)"
368 case $SH in dash|mksh) exit ;; esac
369
370 a=(1)
371 set -x
372 declare a+=(2)
373 ## STDERR:
374 + declare a+=(2)
375 ## END
376 ## OK bash STDERR:
377 + a+=('2')
378 + declare a
379 ## END
380 ## N-I dash/mksh STDERR:
381 ## END
382
383
384 #### Regression: xtrace for "a+=(v)"
385 case $SH in dash|mksh) exit ;; esac
386
387 a=(1)
388 set -x
389 a+=(2)
390 ## STDERR:
391 + a+=(2)
392 ## END
393 ## N-I dash/mksh STDERR:
394 ## END