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

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

当前回答

str='hello\nworld'
$ echo | sed "i$str"
hello
world

其他回答

str='hello\nworld'
$ echo | sed "i$str"
hello
world

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

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

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

印刷品

hello
world

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

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

您还可以执行以下操作:

echo "hello
world"

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

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

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

我只使用echo而不使用任何参数:

echo "Hello"
echo
echo "World"