Chris's Wiki :: blog/unix/SaneHereDocumentsPipelines Commentshttps://utcc.utoronto.ca/~cks/space/blog/unix/SaneHereDocumentsPipelines?atomcommentsDWiki2018-04-20T08:38:36ZRecent comments in Chris's Wiki :: blog/unix/SaneHereDocumentsPipelines.By Aristotle Pagaltzis on /blog/unix/SaneHereDocumentsPipelinestag:CSpace:blog/unix/SaneHereDocumentsPipelines:4fdb680bc9f71296c5b66fecab956558b142d61bAristotle Pagaltzishttp://plasmasturm.org/<div class="wikitext"><p>That is nice too. It evaluates the heredoc every time it’s mentioned, and if you need that, it cleanly beats my suggestion. Note that it can also neatly be made into a one-liner:</p>
<pre>
foo_data() { cat <<EOF ; }
foo
EOF
</pre>
</div>2018-04-20T08:38:36ZBy John Wiersba on /blog/unix/SaneHereDocumentsPipelinestag:CSpace:blog/unix/SaneHereDocumentsPipelines:65b390cfa4daad7cbd7f519be074615f4040cfa4John Wiersba<div class="wikitext"><p>You can also use shell functions to isolate the heredoc and make your pipeline more readable:</p>
<pre>
foo_data() {
cat <<EOF
foo
EOF
}
foo_data | sed | fmt
</pre>
</div>2018-04-19T17:46:40ZBy Aristotle Pagaltzis on /blog/unix/SaneHereDocumentsPipelinestag:CSpace:blog/unix/SaneHereDocumentsPipelines:7c4375142ca667d1788bd7b9a4e297461abae3edAristotle Pagaltzishttp://plasmasturm.org/<div class="wikitext"><blockquote><p>This doesn't work well if there's some paragraphs that I want to include only some of the time, though; then I should still be using a subshell.</p>
</blockquote>
<p>Wouldn’t you just interleave <code>fmt</code> and <code>cat</code> calls, each with smaller heredocs, in that case? And then put the whole thing in a subshell if necessary. I.e.,</p>
<pre>
(
cat <<EOF
...
EOF
fmt <<EOF
...
EOF
) | sed | whatever
</pre>
<blockquote><p>Having written that out, I've just come to the obvious realization that for simple cases I can just directly use <code>fmt</code> with a here document:</p>
</blockquote>
<p>I was going to object to the useless use of <code>cat</code> until I got to that part. 😊</p>
<p>Note that you can get rid of both the subshell and the <code>cat</code> by using <code>read</code> and heredocs for your multiline values and then using herestrings to pass them.</p>
<pre>
read -d '' message <<EOF
your here document goes here
with as much as you want.
EOF
<<< "$message" sed | whatever
</pre>
<p>(Since you can put redirections anywhere in a command, when I have a pipe, I like to move them to the front.)</p>
<p>This also has the advantage that you can give your literal a name… and the disadvantage that you must give it a name.</p>
</div>2018-04-19T08:36:41ZFrom 193.219.181.219 on /blog/unix/SaneHereDocumentsPipelinestag:CSpace:blog/unix/SaneHereDocumentsPipelines:a2e071cf0423133d18546e677c9c9a5db17e6265From 193.219.181.219<div class="wikitext"><p>...which, on the second thought, doesn't actually reduce the number of shell processes much anyway if it's in a pipeline. (And some shells might optimize away the subshell, too.)</p>
<p>Also, zsh has a neat feature which lets you do <code>(< file</code>) or <code>(<<< "text"</code>) or other kinds of redirections without invoking <code>cat</code> – the shell itself does the rest. It doesn't seem to be available in sh or bash however.</p>
</div>2018-04-19T04:32:32ZFrom 78.58.206.110 on /blog/unix/SaneHereDocumentsPipelinestag:CSpace:blog/unix/SaneHereDocumentsPipelines:b45cfd9edb299b168d76b7f1d4ecc95f58f56c31From 78.58.206.110<div class="wikitext"><p>If you just need grouping without a subshell, <code>{ ... }</code> should work. It's called ”brace_group" in sh grammar.</p>
</div>2018-04-19T04:05:29Z