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 echo $y
25 ## END
26
27 ### xtrace with whitespace and quotes
28 set -o xtrace
29 echo '1 2' \' \"
30 ## STDOUT:
31 1 2 ' "
32 ## STDERR:
33 + echo '1 2' \' '"'
34 ## BUG dash STDERR:
35 + echo 1 2 ' "
36 ## END
37
38 ### CASE: xtrace with newlines
39 # bash and dash trace this badly. They print literal newlines, which I don't
40 # want.
41 set -x
42 echo $'[\n]'
43 # STDOUT:
44 [
45 ]
46 # stderr-json: "+ echo $'[\\n]'\n"
47 # OK bash stderr-json: "+ echo '[\n]'\n"
48 # N-I dash stdout-json: "$[\n]\n"
49 # N-I dash stderr-json: "+ echo $[\\n]\n"
50
51 ### xtrace written before command executes
52 set -x
53 echo one >&2
54 echo two >&2
55 ## stdout-json: ""
56 ## STDERR:
57 + echo one
58 one
59 + echo two
60 two
61 ## OK mksh STDERR:
62 # mksh traces redirects!
63 + >&2
64 + echo one
65 one
66 + >&2
67 + echo two
68 two
69 ## END
70
71 ### PS4 is scoped
72 set -x
73 echo one
74 f() {
75 local PS4='- '
76 echo func;
77 }
78 f
79 echo two
80 ## STDERR:
81 + echo one
82 + f
83 + local 'PS4=- '
84 - echo func
85 + echo two
86 ## OK dash STDERR:
87 # dash loses information about spaces! There is a trailing space, but you
88 # can't see it.
89 + echo one
90 + f
91 + local PS4=-
92 - echo func
93 + echo two
94 ## OK mksh STDERR:
95 # local gets turned into typeset
96 + echo one
97 + f
98 + typeset 'PS4=- '
99 - echo func
100 + echo two
101 ## BUG osh STDERR:
102 # local gets turned into typeset
103 + echo one
104 + f
105 - echo func
106 + echo two
107 ## END
108
109 ### xtrace with variables in PS4
110 PS4='+$x:'
111 set -o xtrace
112 x=1
113 echo one
114 x=2
115 echo two
116 ## STDOUT:
117 one
118 two
119 ## STDERR:
120 +:x=1
121 +1:echo one
122 +1:x=2
123 +2:echo two
124 ## OK mksh STDERR:
125 # mksh has trailing spaces
126 +:x=1
127 +1:echo one
128 +1:x=2
129 +2:echo two
130 ## OK dash STDERR:
131 # dash evaluates it earlier
132 +1:x=1
133 +1:echo one
134 +2:x=2
135 +2:echo two
136 ## OK osh STDERR:
137 # dash evaluates it earlier
138 +1:echo one
139 +2:echo two
140 ## END
141
142 ### PS4 with unterminated ${
143 x=1
144 PS4='+${x'
145 set -o xtrace
146 echo one
147 echo status=$?
148 ## STDOUT:
149 one
150 status=0
151 ## END
152 # mksh and dash both fail. bash prints errors to stderr.
153 # OK dash stdout-json: ""
154 # OK dash status: 2
155 # OK mksh stdout-json: ""
156 # OK mksh status: 1
157
158 ### PS4 with unterminated $(
159 # osh is not making this a proper syntax error
160 x=1
161 PS4='+$(x'
162 set -o xtrace
163 echo one
164 echo status=$?
165 ## STDOUT:
166 one
167 status=0
168 ## END
169 # mksh and dash both fail. bash prints errors to stderr.
170 # OK dash stdout-json: ""
171 # OK dash status: 2
172 # OK mksh stdout-json: ""
173 # OK mksh status: 1
174