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

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

当前回答

使用jq:

$ jq -nr '"Hello,\nWorld"'
Hello,
World

其他回答

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

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

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

其他解决方案:

在某些情况下,您必须回显多行长内容(例如代码/配置)

例如:

生成代码/配置的Bash脚本

echo-e,printf可能有一些限制

您可以使用一些特殊字符作为占位符作为换行符(例如~),并在使用tr创建文件后替换它:

echo ${content} | tr '~' '\n' > $targetFile

它需要调用另一个程序(tr),这应该很好,IMO。

确保你在巴什。

$ echo $0
bash

这四种方式对我都有效:

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

这让我到了那里。。。。

outstuff=RESOURCE_GROUP=[$RESOURCE_GROUP]\\nAKS_CLUSTER_NAME=[$AKS_CLUSTER_NAME]\\nREGION_NAME=[$REGION_NAME]\\nVERSION=[$VERSION]\\nSUBNET-ID=[$SUBNET_ID]
printf $outstuff

产量:

RESOURCE_GROUP=[akswork-rg]
AKS_CLUSTER_NAME=[aksworkshop-804]
REGION_NAME=[eastus]
VERSION=[1.16.7]
SUBNET-ID=[/subscriptions/{subidhere}/resourceGroups/makeakswork-rg/providers/Microsoft.Network/virtualNetworks/aks-vnet/subnets/aks-subnet]
echo $'hello\nworld'

印刷品

hello
world

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

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