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