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

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

当前回答

改用printf:

printf "hello\nworld\n"

printf在不同环境中的表现比echo更一致。

其他回答

简单地键入

echo

获得一条新线路

改用printf:

printf "hello\nworld\n"

printf在不同环境中的表现比echo更一致。

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

echo -e 'Hello,\nWorld!'

结果如下:

Hello,
World!

有时您可以传递由空格分隔的多个字符串,它将被解释为\n。

例如,当为多行通知使用shell脚本时:

#!/bin/bash
notify-send 'notification success' 'another line' 'time now '`date +"%s"`

这最好作为

x="\n"
echo -ne $x

-e选项将解释转义序列的反斜杠-n选项将删除输出中的尾随换行符

PS:命令echo的效果是始终在输出中包含一个尾随换行符,因此需要-n来关闭该功能(并使其不那么混乱)