1 #!/usr/bin/env bash
2 #
3 # xtrace test. Test PS4 and line numbers, etc.
4
5 #### set -o verbose prints unevaluated code
6 set -o verbose
7 x=foo
8 y=bar
9 echo $x
10 echo $(echo $y)
11 ## STDOUT:
12 foo
13 bar
14 ## STDERR:
15 x=foo
16 y=bar
17 echo $x
18 echo $(echo $y)
19 ## OK bash STDERR:
20 x=foo
21 y=bar
22 echo $x
23 echo $(echo $y)
24 ## END
25
26 #### xtrace with unprintable chars
27 case $SH in (dash) exit ;; esac
28
29 s=$'a\x03b\004c\x00d'
30 set -o xtrace
31 echo "$s"
32 ## stdout-repr: 'a\x03b\x04c\x00d\n'
33 ## STDERR:
34 + echo $'a\x03b\x04c\x00d'
35 ## END
36 ## OK bash stdout-repr: 'a\x03b\x04c\n'
37 ## OK bash stderr-repr: "+ echo $'a\\003b\\004c'\n"
38
39 # nonsensical output?
40 ## BUG mksh stdout-repr: 'a;\x04c\r\n'
41 ## BUG mksh stderr-repr: "+ echo $'a;\\004c\\r'\n"
42 ## N-I dash stdout-json: ""
43 ## N-I dash stderr-json: ""
44
45 #### xtrace with unicode chars
46 case $SH in (dash) exit ;; esac
47
48 mu1='[μ]'
49 mu2=$'[\u03bc]'
50
51 set -o xtrace
52 echo "$mu1" "$mu2"
53
54 ## STDOUT:
55 [μ] [μ]
56 ## END
57 ## STDERR:
58 + echo '[μ]' '[μ]'
59 ## END
60 ## N-I dash stdout-json: ""
61 ## N-I dash stderr-json: ""
62
63 #### xtrace with tabs
64 case $SH in (dash) exit ;; esac
65
66 set -o xtrace
67 echo $'[\t]'
68 ## stdout-json: "[\t]\n"
69 ## STDERR:
70 + echo $'[\t]'
71 ## END
72 # this is a bug because it's hard to see
73 ## BUG bash stderr-json: "+ echo '[\t]'\n"
74 ## N-I dash stdout-json: ""
75 ## N-I dash stderr-json: ""
76
77 #### xtrace with whitespace, quotes, and backslash
78 set -o xtrace
79 echo '1 2' \' \" \\
80 ## STDOUT:
81 1 2 ' " \
82 ## END
83
84 # Oil is different bceause backslashes require $'\\' and not '\', but that's OK
85 ## STDERR:
86 + echo '1 2' $'\'' '"' $'\\'
87 ## END
88
89 ## OK bash/mksh STDERR:
90 + echo '1 2' \' '"' '\'
91 ## END
92
93 ## BUG dash STDERR:
94 + echo 1 2 ' " \
95 ## END
96
97 #### xtrace with newlines
98 # bash and dash trace this badly. They print literal newlines, which I don't
99 # want.
100 set -x
101 echo $'[\n]'
102 ## STDOUT:
103 [
104 ]
105 ## STDERR:
106 + echo $'[\n]'
107 ## END
108 # bash has ugly output that spans lines
109 ## OK bash STDERR:
110 + echo '[
111 ]'
112 ## END
113 ## N-I dash stdout-json: "$[\n]\n"
114 ## N-I dash stderr-json: "+ echo $[\\n]\n"
115
116 #### xtrace written before command executes
117 set -x
118 echo one >&2
119 echo two >&2
120 ## stdout-json: ""
121 ## STDERR:
122 + echo one
123 one
124 + echo two
125 two
126 ## OK mksh STDERR:
127 # mksh traces redirects!
128 + >&2
129 + echo one
130 one
131 + >&2
132 + echo two
133 two
134 ## END
135
136 #### PS4 is scoped
137 set -x
138 echo one
139 f() {
140 local PS4='- '
141 echo func;
142 }
143 f
144 echo two
145 ## STDERR:
146 + echo one
147 + f
148 + local 'PS4=- '
149 - echo func
150 + echo two
151 ## OK dash STDERR:
152 # dash loses information about spaces! There is a trailing space, but you
153 # can't see it.
154 + echo one
155 + f
156 + local PS4=-
157 - echo func
158 + echo two
159 ## OK mksh STDERR:
160 # local gets turned into typeset
161 + echo one
162 + f
163 + typeset 'PS4=- '
164 - echo func
165 + echo two
166 ## BUG osh STDERR:
167 # local gets turned into typeset
168 + echo one
169 + f
170 - echo func
171 + echo two
172 ## END
173
174 #### xtrace with variables in PS4
175 PS4='+$x:'
176 set -o xtrace
177 x=1
178 echo one
179 x=2
180 echo two
181 ## STDOUT:
182 one
183 two
184 ## STDERR:
185 +:x=1
186 +1:echo one
187 +1:x=2
188 +2:echo two
189 ## OK mksh STDERR:
190 # mksh has trailing spaces
191 +:x=1
192 +1:echo one
193 +1:x=2
194 +2:echo two
195 ## OK dash STDERR:
196 # dash evaluates it earlier
197 +1:x=1
198 +1:echo one
199 +2:x=2
200 +2:echo two
201 ## OK osh STDERR:
202 # dash evaluates it earlier
203 +1:echo one
204 +2:echo two
205 ## END
206
207 #### PS4 with unterminated ${
208 # osh shows inline error; maybe fail like dash/mksh?
209 x=1
210 PS4='+${x'
211 set -o xtrace
212 echo one
213 echo status=$?
214 ## STDOUT:
215 one
216 status=0
217 ## END
218 # mksh and dash both fail. bash prints errors to stderr.
219 ## OK dash stdout-json: ""
220 ## OK dash status: 2
221 ## OK mksh stdout-json: ""
222 ## OK mksh status: 1
223
224 #### PS4 with unterminated $(
225 # osh shows inline error; maybe fail like dash/mksh?
226 x=1
227 PS4='+$(x'
228 set -o xtrace
229 echo one
230 echo status=$?
231 ## STDOUT:
232 one
233 status=0
234 ## END
235 # mksh and dash both fail. bash prints errors to stderr.
236 ## OK dash stdout-json: ""
237 ## OK dash status: 2
238 ## OK mksh stdout-json: ""
239 ## OK mksh status: 1
240
241 #### PS4 with runtime error
242 # osh shows inline error; maybe fail like dash/mksh?
243 x=1
244 PS4='+oops $(( 1 / 0 )) \$'
245 set -o xtrace
246 echo one
247 echo status=$?
248 ## STDOUT:
249 one
250 status=0
251 ## END
252 # mksh and dash both fail. bash prints errors to stderr.
253 ## OK dash stdout-json: ""
254 ## OK dash status: 2
255 ## OK mksh stdout-json: ""
256 ## OK mksh status: 1
257