ThomasReggi
ThomasReggi

Reputation: 59505

Trying to collect output from multiple statments

Is there a way to wrap all of this write (pun intended) no only ``` is in temp.md

echo "\`\`\`" && cat temp.txt && echo "\`\`\`" > temp.md

Upvotes: 0

Views: 35

Answers (2)

John1024
John1024

Reputation: 113964

It is simpler (no backslashes needed) to use single-quotes:

{ echo '```' && cat temp.txt && echo '```'; }  >temp.md

Alternatively, if you want finer control over the output format, printf is handy:

printf '```\n%s\n```\n' "$(cat temp.txt)" >temp.md

Why use single-quotes?

From man bash:

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

In other words, you can put anything inside single quotes, except for a single quote, and it will be preserved unchanged. No escapes needed.

By contrast, the shell processes characters inside of double quotes. From man bash:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.

For example, inside double quotes, the shell will perform variable substitution, command substitution, and arithmetic expansion. Do not use double quotes unless you want these things to happen.

Upvotes: 2

ThomasReggi
ThomasReggi

Reputation: 59505

Found it!

(echo "\`\`\`" && cat temp.txt && echo "\`\`\`") > temp.md

Upvotes: 0

Related Questions