OILS / spec / builtin-echo.test.sh View on Github | oilshell.org

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