1 ## compare_shells: bash mksh zsh ash
2
3 # dash doesn't have echo -e, $'', etc.
4
5 # TODO: fix J8 bug causing failure
6
7 # Cross-cutting test of serialization formats. That is, what J8 Notation
8 # should fix.
9 #
10 # TODO: Also see spec/xtrace for another use case.
11
12 #### printf %q newline
13 case $SH in (ash) return ;; esac # yash and ash don't implement this
14
15 newline=$'one\ntwo'
16 printf '%q\n' "$newline"
17
18 quoted="$(printf '%q\n' "$newline")"
19 restored=$(eval "echo $quoted")
20 test "$newline" = "$restored" && echo roundtrip-ok
21
22 ## STDOUT:
23 $'one\ntwo'
24 roundtrip-ok
25 ## END
26 ## OK mksh STDOUT:
27 'one'$'\n''two'
28 roundtrip-ok
29 ## END
30 ## OK zsh STDOUT:
31 one$'\n'two
32 roundtrip-ok
33 ## END
34 ## N-I ash stdout-json: ""
35
36 #### printf %q spaces
37 case $SH in (ash) return ;; esac # yash and ash don't implement this
38
39 # bash does a weird thing and uses \
40
41 spaces='one two'
42 printf '%q\n' "$spaces"
43
44 ## STDOUT:
45 'one two'
46 ## END
47 ## OK bash/zsh STDOUT:
48 one\ two
49 ## END
50 ## N-I ash stdout-json: ""
51
52 #### printf %q quotes
53 case $SH in (ash) return ;; esac # yash and ash don't implement %q
54
55 quotes=\'\"
56 printf '%q\n' "$quotes"
57
58 quoted="$(printf '%q\n' "$quotes")"
59 restored=$(eval "echo $quoted")
60 test "$quotes" = "$restored" && echo roundtrip-ok
61
62 ## STDOUT:
63 \'\"
64 roundtrip-ok
65 ## END
66 ## OK osh STDOUT:
67 $'\'"'
68 roundtrip-ok
69 ## END
70 ## BUG mksh STDOUT:
71 ''\''"'
72 roundtrip-ok
73 ## END
74 ## N-I ash stdout-json: ""
75
76 #### printf %q unprintable
77 case $SH in (ash) return ;; esac # yash and ash don't implement this
78
79 unprintable=$'\xff'
80 printf '%q\n' "$unprintable"
81
82 # bash and zsh agree
83 ## STDOUT:
84 $'\377'
85 ## END
86 ## OK osh STDOUT:
87 $'\xff'
88 ## END
89 ## BUG mksh STDOUT:
90 ''$'\377'
91 ## END
92 ## N-I ash stdout-json: ""
93
94 #### printf %q unicode
95 case $SH in (ash) return ;; esac # yash and ash don't implement this
96
97 unicode=$'\u03bc'
98 unicode=$'\xce\xbc' # does the same thing
99
100 printf '%q\n' "$unicode"
101
102 # Oil issue: we have quotes. Isn't that OK?
103 ## STDOUT:
104 μ
105 ## END
106 ## OK osh STDOUT:
107 'μ'
108 ## END
109 ## N-I ash stdout-json: ""
110
111 #### printf %q invalid unicode
112 case $SH in (ash) return ;; esac
113
114 # Hm bash/mksh/zsh understand these. They are doing decoding and error
115 # recovery! inspecting the bash source seems to confirm this.
116 unicode=$'\xce'
117 printf '%q\n' "$unicode"
118
119 unicode=$'\xce\xce\xbc'
120 printf '%q\n' "$unicode"
121
122 unicode=$'\xce\xbc\xce'
123 printf '%q\n' "$unicode"
124
125 case $SH in (mksh) return ;; esac # it prints unprintable chars here!
126
127 unicode=$'\xcea'
128 printf '%q\n' "$unicode"
129 unicode=$'a\xce'
130 printf '%q\n' "$unicode"
131 ## STDOUT:
132 $'\xce'
133 $'\xceμ'
134 $'μ\xce'
135 $'\xcea'
136 $'a\xce'
137 ## END
138 ## OK bash STDOUT:
139 $'\316'
140 $'\316μ'
141 $'μ\316'
142 $'\316a'
143 $'a\316'
144 ## END
145 ## BUG mksh STDOUT:
146 ''$'\316'
147 ''$'\316''μ'
148 'μ'$'\316'
149 ## END
150 ## OK zsh STDOUT:
151 $'\316'
152 $'\316'μ
153 μ$'\316'
154 $'\316'a
155 a$'\316'
156 ## END
157 ## N-I ash stdout-json: ""
158
159 #### set
160 case $SH in (zsh) return ;; esac # zsh doesn't make much sense
161
162 zz=$'one\ntwo'
163
164 set | grep zz
165 ## STDOUT:
166 zz=$'one\ntwo'
167 ## END
168 ## OK ash stdout-json: "zz='one\n"
169 ## BUG zsh stdout-json: ""
170
171
172 #### declare
173 case $SH in (ash|zsh) return ;; esac # zsh doesn't make much sense
174
175 zz=$'one\ntwo'
176
177 typeset | grep zz
178 typeset -p zz
179
180 ## STDOUT:
181 zz=$'one\ntwo'
182 declare -- zz=$'one\ntwo'
183 ## END
184
185 # bash uses a different format for 'declare' and 'declare -p'!
186 ## OK bash STDOUT:
187 zz=$'one\ntwo'
188 declare -- zz="one
189 two"
190 ## END
191 ## OK mksh STDOUT:
192 typeset zz
193 typeset zz=$'one\ntwo'
194 ## BUG zsh stdout-json: ""
195 ## N-I ash stdout-json: ""
196
197 #### ${var@Q}
198 case $SH in (zsh|ash) exit ;; esac
199
200 zz=$'one\ntwo \u03bc'
201
202 # weirdly, quoted and unquoted aren't different
203 echo ${zz@Q}
204 echo "${zz@Q}"
205 ## STDOUT:
206 $'one\ntwo μ'
207 $'one\ntwo μ'
208 ## END
209 ## OK mksh STDOUT:
210 $'one
211 two μ'
212 $'one
213 two μ'
214 ## END
215 ## N-I ash/zsh stdout-json: ""
216
217 #### xtrace
218 zz=$'one\ntwo'
219 set -x
220 echo "$zz"
221 ## STDOUT:
222 one
223 two
224 ## END
225 ## STDERR:
226 + echo $'one\ntwo'
227 ## END
228 ## OK bash/ash STDERR:
229 + echo 'one
230 two'
231 ## END
232 ## OK zsh STDERR:
233 +zsh:3> echo 'one
234 two'
235 ## END
236