试图从多个语句中收集输出

Trying to collect output from multiple statments

有没有办法包装所有这些写的东西(双关语意)不只有```在temp.md

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

找到了!

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

使用单引号更简单(不需要反斜杠):

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

或者,如果您想更好地控制输出格式,printf 很方便:

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

为什么要使用单引号?

来自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.

换句话说,你可以把任何东西放在单引号里,除了单引号,它会被保留不变。无需转义。

相比之下,shell 处理双引号内的字符。来自 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, !.

例如,在双引号内,shell 将执行变量替换、命令替换和算术扩展。除非您希望这些事情发生,否则不要使用双引号。