1 # NOTE: not enabled, see test/spec.sh here-doc
2 # oils_failures_allowed: 2
3 # compare_shells: dash bash mksh
4
5 #### Here string
6 cat <<< 'hi'
7 ## STDOUT:
8 hi
9 ## END
10 ## N-I dash stdout-json: ""
11 ## N-I dash status: 2
12
13 #### Here string with $
14 cat <<< $'one\ntwo\n'
15 ## STDOUT:
16 one
17 two
18
19 ## END
20 ## N-I dash stdout-json: ""
21 ## N-I dash status: 2
22
23 #### Here redirect with explicit descriptor
24 # A space between 0 and <<EOF causes it to pass '0' as an arg to cat.
25 cat 0<<EOF
26 one
27 EOF
28 ## stdout: one
29
30 #### Here doc from another input file descriptor
31 # NOTE: OSH fails on descriptor 9, but not descriptor 8? Is this because of
32 # the Python VM? How to inspect state?
33 read_from_fd.py 8 8<<EOF
34 here doc on descriptor
35 EOF
36 ## stdout: 8: here doc on descriptor
37
38 #### Multiple here docs with different descriptors
39 read_from_fd.py 0 3 <<EOF 3<<EOF3
40 fd0
41 EOF
42 fd3
43 EOF3
44 ## STDOUT:
45 0: fd0
46 3: fd3
47 ## END
48
49 #### Here doc with bad var delimiter
50 # Most shells accept this, but OSH is stricter.
51 cat <<${a}
52 here
53 ${a}
54 ## stdout: here
55 ## OK osh stdout-json: ""
56 ## OK osh status: 2
57
58 #### Here doc with bad comsub delimiter
59 # bash is OK with this; dash isn't. Should be a parse error.
60 cat <<$(a)
61 here
62 $(a)
63 ## stdout-json: ""
64 ## status: 2
65 ## BUG bash stdout: here
66 ## BUG bash status: 0
67 ## OK mksh status: 1
68
69 #### Here doc and < redirect -- last one wins
70
71 echo hello >$TMP/hello.txt
72
73 cat <<EOF <$TMP/hello.txt
74 here
75 EOF
76 ## stdout: hello
77
78 #### < redirect and here doc -- last one wins
79
80 echo hello >$TMP/hello.txt
81
82 cat <$TMP/hello.txt <<EOF
83 here
84 EOF
85 ## stdout: here
86
87 #### Here doc with var sub, command sub, arith sub
88 var=v
89 cat <<EOF
90 var: ${var}
91 command: $(echo hi)
92 arith: $((1+2))
93 EOF
94 ## STDOUT:
95 var: v
96 command: hi
97 arith: 3
98 ## END
99
100 #### Here doc in middle. And redirects in the middle.
101 # This isn't specified by the POSIX grammar, but it's accepted by both dash and
102 # bash!
103 echo foo > _tmp/foo.txt
104 echo bar > _tmp/bar.txt
105 cat <<EOF 1>&2 _tmp/foo.txt - _tmp/bar.txt
106 here
107 EOF
108 ## STDERR:
109 foo
110 here
111 bar
112 ## END
113
114 #### Here doc line continuation
115 cat <<EOF \
116 ; echo two
117 one
118 EOF
119 ## STDOUT:
120 one
121 two
122 ## END
123
124 #### Here doc with quote expansion in terminator
125 cat <<'EOF'"2"
126 one
127 two
128 EOF2
129 ## STDOUT:
130 one
131 two
132 ## END
133
134 #### Here doc with multiline double quoted string
135 cat <<EOF; echo "two
136 three"
137 one
138 EOF
139 ## STDOUT:
140 one
141 two
142 three
143 ## END
144
145 #### Two here docs -- first is ignored; second ones wins!
146 <<EOF1 cat <<EOF2
147 hello
148 EOF1
149 there
150 EOF2
151 ## stdout: there
152
153 #### Here doc with line continuation, then pipe. Syntax error.
154 cat <<EOF \
155 1
156 2
157 3
158 EOF
159 | tac
160 ## status: 2
161 ## OK mksh status: 1
162
163 #### Here doc with pipe on first line
164 cat <<EOF | tac
165 1
166 2
167 3
168 EOF
169 ## STDOUT:
170 3
171 2
172 1
173 ## END
174
175 #### Here doc with pipe continued on last line
176 cat <<EOF |
177 1
178 2
179 3
180 EOF
181 tac
182 ## STDOUT:
183 3
184 2
185 1
186 ## END
187
188 #### Here doc with builtin 'read'
189 # read can't be run in a subshell.
190 read v1 v2 <<EOF
191 val1 val2
192 EOF
193 echo =$v1= =$v2=
194 ## stdout: =val1= =val2=
195
196 #### Compound command here doc
197 while read line; do
198 echo X $line
199 done <<EOF
200 1
201 2
202 3
203 EOF
204 ## STDOUT:
205 X 1
206 X 2
207 X 3
208 ## END
209
210
211 #### Here doc in while condition and here doc in body
212 while cat <<E1 && cat <<E2; do cat <<E3; break; done
213 1
214 E1
215 2
216 E2
217 3
218 E3
219 ## STDOUT:
220 1
221 2
222 3
223 ## END
224
225 #### Here doc in while condition and here doc in body on multiple lines
226 while cat <<E1 && cat <<E2
227 1
228 E1
229 2
230 E2
231 do
232 cat <<E3
233 3
234 E3
235 break
236 done
237 ## STDOUT:
238 1
239 2
240 3
241 ## END
242
243 #### Here doc in while loop split up more
244 while cat <<E1
245 1
246 E1
247
248 cat <<E2
249 2
250 E2
251
252 do
253 cat <<E3
254 3
255 E3
256 break
257 done
258 ## STDOUT:
259 1
260 2
261 3
262 ## END
263
264 #### Mixing << and <<-
265 cat <<-EOF; echo --; cat <<EOF2
266 one
267 EOF
268 two
269 EOF2
270 ## STDOUT:
271 one
272 --
273 two
274 ## END
275
276
277
278 #### Two compound commands with two here docs
279 while read line; do echo X $line; done <<EOF; echo ==; while read line; do echo Y $line; done <<EOF2
280 1
281 2
282 EOF
283 3
284 4
285 EOF2
286 ## STDOUT:
287 X 1
288 X 2
289 ==
290 Y 3
291 Y 4
292 ## END
293
294 #### Function def and execution with here doc
295 fun() { cat; } <<EOF; echo before; fun; echo after
296 1
297 2
298 EOF
299 ## STDOUT:
300 before
301 1
302 2
303 after
304 ## END
305
306 #### Here doc as command prefix
307 <<EOF tac
308 1
309 2
310 3
311 EOF
312 ## STDOUT:
313 3
314 2
315 1
316 ## END
317
318 # NOTE that you can have redirection AFTER the here doc thing. And you don't
319 # need a space! Those are operators.
320 #
321 # POSIX doesn't seem to have this? They have io_file, which is for
322 # filenames, and io_here, which is here doc. But about 1>&2 syntax? Geez.
323 #### Redirect after here doc
324 cat <<EOF 1>&2
325 out
326 EOF
327 ## stderr: out
328
329 #### here doc stripping tabs
330 cat <<-EOF
331 1
332 2
333 3 # 2 tabs are both stripped
334 4 # spaces are preserved
335 EOF
336 ## STDOUT:
337 1
338 2
339 3 # 2 tabs are both stripped
340 4 # spaces are preserved
341 ## END
342
343 #### Here doc within subshell with boolean
344 [[ $(cat <<EOF
345 foo
346 EOF
347 ) == foo ]]; echo $?
348 ## stdout: 0
349 ## N-I dash stdout: 127
350
351 #### Here Doc in if condition
352 if cat <<EOF; then
353 here doc in IF CONDITION
354 EOF
355 echo THEN executed
356 fi
357 ## STDOUT:
358 here doc in IF CONDITION
359 THEN executed
360 ## END
361
362 #### Nested here docs which are indented
363 cat <<- EOF
364 outside
365 $(cat <<- INSIDE
366 inside
367 INSIDE
368 )
369 EOF
370 ## STDOUT:
371 outside
372 inside
373 ## END
374
375 #### Multiple here docs in pipeline
376
377 # The second instance reads its stdin from the pipe, and fd 5 from a here doc.
378 read_from_fd.py 3 3<<EOF3 | read_from_fd.py 0 5 5<<EOF5
379 fd3
380 EOF3
381 fd5
382 EOF5
383
384 echo ok
385
386 ## STDOUT:
387 0: 3: fd3
388 5: fd5
389 ok
390 ## END
391
392 #### Multiple here docs in pipeline on multiple lines
393 # SKIPPED: hangs with osh on Debian
394 # The second instance reads its stdin from the pipe, and fd 5 from a here doc.
395 read_from_fd.py 3 3<<EOF3 |
396 fd3
397 EOF3
398 read_from_fd.py 0 5 5<<EOF5
399 fd5
400 EOF5
401
402 echo ok
403
404 ## STDOUT:
405 0: 3: fd3
406 5: fd5
407 ok
408 ## END
409