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

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

当前回答

要打印带有回音的新行,请使用:

echo

or

echo -e '\n'

其他回答

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

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

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

确保你在巴什。

$ echo $0
bash

这四种方式对我都有效:

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

使用jq:

$ jq -nr '"Hello,\nWorld"'
Hello,
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]

如果有人发现自己的头撞在墙上,试图弄清楚为什么同事的脚本不会打印新行,那么请注意:

#!/bin/bash
function GET_RECORDS()
{
   echo -e "starting\n the process";
}

echo $(GET_RECORDS);

如上所述,方法的实际运行本身可能会被一个回声所包裹,该回声会取代方法本身中的任何回声。显然,为了简洁起见,我淡化了这一点。这不是那么容易发现的!

然后,您可以通知您的同事,执行功能的更好方法如下:

#!/bin/bash
function GET_RECORDS()
{
   echo -e "starting\n the process";
}

GET_RECORDS;