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

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

当前回答

Bash 4.4中添加了一个新的参数扩展,用于解释转义序列:

${parameter@operator}-E操作员展开是一个字符串,它是参数的值反斜杠转义序列扩展为$“…”引号机械装置

$ foo='hello\nworld'
$ echo "${foo@E}"
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:
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,"
echo ""
echo "World!"

您还可以执行以下操作:

echo "hello
world"

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

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

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