OILS / spec / builtin-echo.test.sh View on Github | oils.pub

320 lines, 128 significant
1## oils_failures_allowed: 0
2## compare_shells: dash bash mksh zsh ash
3
4# TODO mapfile options: -c, -C, -u, etc.
5
6#### echo dashes
7echo -
8echo --
9echo ---
10## STDOUT:
11-
12--
13---
14## END
15## BUG zsh STDOUT:
16
17--
18---
19## END
20
21#### echo backslashes
22echo \\
23echo '\'
24echo '\\'
25echo "\\"
26## STDOUT:
27\
28\
29\\
30\
31## BUG dash/mksh/zsh STDOUT:
32\
33\
34\
35\
36## END
37
38#### echo -e backslashes
39echo -e \\
40echo -e '\'
41echo -e '\\'
42echo -e "\\"
43echo
44
45# backslash at end of line
46echo -e '\
47line2'
48## STDOUT:
49\
50\
51\
52\
53
54\
55line2
56## N-I dash STDOUT:
57-e \
58-e \
59-e \
60-e \
61
62-e \
63line2
64## END
65
66#### echo builtin should disallow typed args - literal
67echo (42)
68## status: 2
69## OK mksh/zsh status: 1
70## STDOUT:
71## END
72
73#### echo builtin should disallow typed args - variable
74var x = 43
75echo (x)
76## status: 2
77## OK mksh/zsh status: 1
78## STDOUT:
79## END
80
81#### echo -en
82echo -en 'abc\ndef\n'
83## STDOUT:
84abc
85def
86## END
87## N-I dash STDOUT:
88-en abc
89def
90
91## END
92
93#### echo -ez (invalid flag)
94# bash differs from the other three shells, but its behavior is possibly more
95# sensible, if you're going to ignore the error. It doesn't make sense for
96# the 'e' to mean 2 different things simultaneously: flag and literal to be
97# printed.
98echo -ez 'abc\n'
99## STDOUT:
100-ez abc\n
101## END
102## OK dash/mksh/zsh STDOUT:
103-ez abc
104
105## END
106
107#### echo -e with embedded newline
108flags='-e'
109case $SH in dash) flags='' ;; esac
110
111echo $flags 'foo
112bar'
113## STDOUT:
114foo
115bar
116## END
117
118#### echo -e line continuation
119flags='-e'
120case $SH in dash) flags='' ;; esac
121
122echo $flags 'foo\
123bar'
124## STDOUT:
125foo\
126bar
127## END
128
129#### echo -e with C escapes
130# https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins
131# not sure why \c is like NUL?
132# zsh doesn't allow \E for some reason.
133echo -e '\a\b\d\e\f'
134## stdout-json: "\u0007\u0008\\d\u001b\u000c\n"
135## N-I dash stdout-json: "-e \u0007\u0008\\d\\e\u000c\n"
136
137#### echo -e with whitespace C escapes
138echo -e '\n\r\t\v'
139## stdout-json: "\n\r\t\u000b\n"
140## N-I dash stdout-json: "-e \n\r\t\u000b\n"
141
142#### \0
143echo -e 'ab\0cd'
144## stdout-json: "ab\u0000cd\n"
145## N-I dash stdout-json: "-e ab\u0000cd\n"
146
147#### \c stops processing input
148flags='-e'
149case $SH in dash) flags='' ;; esac
150
151echo $flags xy 'ab\cde' 'zzz'
152## stdout-json: "xy ab"
153## N-I mksh stdout-json: "xy abde zzz"
154
155#### echo -e with hex escape
156echo -e 'abcd\x65f'
157## STDOUT:
158abcdef
159## END
160## N-I dash STDOUT:
161-e abcd\x65f
162## END
163
164#### echo -e with octal escape
165flags='-e'
166case $SH in dash) flags='' ;; esac
167
168echo $flags 'abcd\044e'
169## STDOUT:
170abcd$e
171## END
172
173#### echo -e with 4 digit unicode escape
174flags='-e'
175case $SH in dash) flags='' ;; esac
176
177echo $flags 'abcd\u0065f'
178## STDOUT:
179abcdef
180## END
181## N-I dash/ash STDOUT:
182abcd\u0065f
183## END
184
185#### echo -e with 8 digit unicode escape
186flags='-e'
187case $SH in dash) flags='' ;; esac
188
189echo $flags 'abcd\U00000065f'
190## STDOUT:
191abcdef
192## END
193## N-I dash/ash STDOUT:
194abcd\U00000065f
195## END
196
197#### \0377 is the highest octal byte
198echo -en '\03777' | od -A n -t x1 | sed 's/ \+/ /g'
199## STDOUT:
200 ff 37
201## END
202## N-I dash STDOUT:
203 2d 65 6e 20 ff 37 0a
204## END
205
206#### \0400 is one more than the highest octal byte
207# It is 256 % 256 which gets interpreted as a NUL byte.
208echo -en '\04000' | od -A n -t x1 | sed 's/ \+/ /g'
209## STDOUT:
210 00 30
211## END
212## BUG ash STDOUT:
213 20 30 30
214## END
215## N-I dash STDOUT:
216 2d 65 6e 20 00 30 0a
217## END
218
219#### \0777 is out of range
220flags='-en'
221case $SH in dash) flags='-n' ;; esac
222
223echo $flags '\0777' | od -A n -t x1 | sed 's/ \+/ /g'
224## STDOUT:
225 ff
226## END
227## BUG mksh STDOUT:
228 c3 bf
229## END
230## BUG ash STDOUT:
231 3f 37
232## END
233
234#### incomplete hex escape
235echo -en 'abcd\x6' | od -A n -c | sed 's/ \+/ /g'
236## STDOUT:
237 a b c d 006
238## END
239## N-I dash STDOUT:
240 - e n a b c d \ x 6 \n
241## END
242
243#### \x
244# I consider mksh and zsh a bug because \x is not an escape
245echo -e '\x' '\xg' | od -A n -c | sed 's/ \+/ /g'
246## STDOUT:
247 \ x \ x g \n
248## END
249## N-I dash STDOUT:
250 - e \ x \ x g \n
251## END
252## BUG mksh/zsh STDOUT:
253 \0 \0 g \n
254## END
255
256#### incomplete octal escape
257flags='-en'
258case $SH in dash) flags='-n' ;; esac
259
260echo $flags 'abcd\04' | od -A n -c | sed 's/ \+/ /g'
261## STDOUT:
262 a b c d 004
263## END
264
265#### incomplete unicode escape
266echo -en 'abcd\u006' | od -A n -c | sed 's/ \+/ /g'
267## STDOUT:
268 a b c d 006
269## END
270## N-I dash STDOUT:
271 - e n a b c d \ u 0 0 6 \n
272## END
273## BUG ash STDOUT:
274 a b c d \ u 0 0 6
275## END
276
277#### \u6
278flags='-en'
279case $SH in dash) flags='-n' ;; esac
280
281echo $flags '\u6' | od -A n -c | sed 's/ \+/ /g'
282## STDOUT:
283 006
284## END
285## N-I dash/ash STDOUT:
286 \ u 6
287## END
288
289#### \0 \1 \8
290# \0 is special, but \1 isn't in bash
291# \1 is special in dash! geez
292flags='-en'
293case $SH in dash) flags='-n' ;; esac
294
295echo $flags '\0' '\1' '\8' | od -A n -c | sed 's/ \+/ /g'
296## STDOUT:
297 \0 \ 1 \ 8
298## END
299## BUG dash/ash STDOUT:
300 \0 001 \ 8
301## END
302
303
304#### echo to redirected directory is an error
305mkdir -p dir
306
307echo foo > ./dir
308echo status=$?
309printf foo > ./dir
310echo status=$?
311
312## STDOUT:
313status=1
314status=1
315## END
316## OK dash STDOUT:
317status=2
318status=2
319## END
320