如何打印新行?这只会打印\n:

$ echo -e "Hello,\nWorld!"
Hello,\nWorld!

当前回答

如果您正在编写脚本,并且将多次将换行符作为其他消息的一部分进行回显,那么一个不错的跨平台解决方案是在变量中添加一个文本换行符,如下所示:

newline='
'

echo "first line${newline}second line"
echo "Error: example error message n${newline}${usage}" >&2 #requires usage to be defined

其他回答

你可以一直做回声“”。

例如

echo "Hello,"
echo ""
echo "World!"
echo $'hello\nworld'

印刷品

hello
world

$“”字符串使用ANSI C报价:

$‘string’形式的单词被特别对待。单词扩展为字符串,并按照ANSI C标准的规定替换反斜杠转义字符。

对于所问的问题(不是特殊字符等),只将双引号改为单引号。

echo -e 'Hello,\nWorld!'

结果如下:

Hello,
World!

我的脚本:

echo "WARNINGS: $warningsFound WARNINGS FOUND:\n$warningStrings

输出:

WARNING : 2 WARNINGS FOUND:\nWarning, found the following local orphaned signature file:

在我的Bash脚本中,我一直像你一样生气,直到我尝试了:

echo "WARNING : $warningsFound WARNINGS FOUND:
$warningStrings"

只需按Enter键即可插入跳转。现在的输出是:

WARNING : 2 WARNINGS FOUND:
Warning, found the following local orphaned signature file:

要打印带有回音的新行,请使用:

echo

or

echo -e '\n'