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