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

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

当前回答

简单地键入

echo

获得一条新线路

其他回答

我的脚本:

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:

如果前面的答案不起作用,并且需要从函数中获取返回值:

function foo()
{
    local v="Dimi";
    local s="";
    .....
    s+="Some message here $v $1\n"
    .....
    echo $s
}

r=$(foo "my message");
echo -e $r;

只有这个技巧在我使用Bash版本开发的Linux系统上有效:

GNU bash, version 2.2.25(1)-release (x86_64-redhat-linux-gnu)

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

echo -e 'Hello,\nWorld!'

结果如下:

Hello,
World!

您还可以执行以下操作:

echo "hello
world"

这在脚本内部和命令行中都有效。

在命令行上,按Shift+Enter键在字符串内进行换行。

这适用于我的macOS和Ubuntu 18.04(仿生海狸)系统。

确保你在巴什。

$ echo $0
bash

这四种方式对我都有效:

echo -e "Hello\nworld"
echo -e 'Hello\nworld'
echo Hello$'\n'world
echo Hello ; echo world