如何将多行json字符串作为主体发布到bash脚本中的curl中

如何将多行json字符串作为主体发布到bash脚本中的curl中,json,bash,curl,Json,Bash,Curl,我有以下bash脚本 #!/bin/bash body=$(cat << EOF { "CreatedBy": "$(hostname -f)", "Id": "$(uuidgen)", "Type": "TestAlertType", "AlertCategory": "NonUrgent" } EOF ) curl -H "Content-Type: application/json" -X POST -d $body ht

我有以下bash脚本

#!/bin/bash

body=$(cat  << EOF
{
    "CreatedBy":  "$(hostname -f)",
    "Id":  "$(uuidgen)",
    "Type":  "TestAlertType",
    "AlertCategory":  "NonUrgent"
    }
EOF
)


curl -H "Content-Type: application/json" -X POST -d $body https://dev.cloudapp.net/v1/
#/bin/bash
body=$(cat这起作用了

curl -H "Content-Type: application/json" -X POST -d "$body" https://dev.cloudapp.net/v1/

您需要引用扩展名:
“$body”
。如果
$body
的内容有/需要新行/回车(例如
\n
),则需要添加
IFS=$”\n
在使用
cat
EOF
创建
$body
之前,否则在以后使用
$body
时,新行将消失,无论是否使用双引号,如
“$body”