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 ## OK dash STDERR:
151 # dash loses information about spaces! There is a trailing space, but you
152 # can't see it.
153 + echo one
154 + f
155 + local PS4=-
156 - echo func
157 + echo two
158 ## OK mksh STDERR:
159 # local gets turned into typeset
160 + echo one
161 + f
162 + typeset 'PS4=- '
163 - echo func
164 + echo two
165 ## BUG osh STDERR:
166 # local gets turned into typeset
167 + echo one
168 + f
169 - echo func
170 + echo two
171 ## END
172
173 #### xtrace with variables in PS4
174 PS4='+$x:'
175 set -o xtrace
176 x=1
177 echo one
178 x=2
179 echo two
180 ## STDOUT:
181 one
182 two
183 ## STDERR:
184 +:x=1
185 +1:echo one
186 +1:x=2
187 +2:echo two
188 ## OK mksh STDERR:
189 # mksh has trailing spaces
190 +:x=1
191 +1:echo one
192 +1:x=2
193 +2:echo two
194 ## OK dash STDERR:
195 # dash evaluates it earlier
196 +1:x=1
197 +1:echo one
198 +2:x=2
199 +2:echo two
200 ## OK osh STDERR:
201 # dash evaluates it earlier
202 +1:echo one
203 +2:echo two
204 ## END
205
206 #### PS4 with unterminated ${
207 # osh shows inline error; maybe fail like dash/mksh?
208 x=1
209 PS4='+${x'
210 set -o xtrace
211 echo one
212 echo status=$?
213 ## STDOUT:
214 one
215 status=0
216 ## END
217 # mksh and dash both fail. bash prints errors to stderr.
218 ## OK dash stdout-json: ""
219 ## OK dash status: 2
220 ## OK mksh stdout-json: ""
221 ## OK mksh status: 1
222
223 #### PS4 with unterminated $(
224 # osh shows inline error; maybe fail like dash/mksh?
225 x=1
226 PS4='+$(x'
227 set -o xtrace
228 echo one
229 echo status=$?
230 ## STDOUT:
231 one
232 status=0
233 ## END
234 # mksh and dash both fail. bash prints errors to stderr.
235 ## OK dash stdout-json: ""
236 ## OK dash status: 2
237 ## OK mksh stdout-json: ""
238 ## OK mksh status: 1
239
240 #### PS4 with runtime error
241 # osh shows inline error; maybe fail like dash/mksh?
242 x=1
243 PS4='+oops $(( 1 / 0 )) \$'
244 set -o xtrace
245 echo one
246 echo status=$?
247 ## STDOUT:
248 one
249 status=0
250 ## END
251 # mksh and dash both fail. bash prints errors to stderr.
252 ## OK dash stdout-json: ""
253 ## OK dash status: 2
254 ## OK mksh stdout-json: ""
255 ## OK mksh status: 1
256