Home > Blockchain >  How to run this bash command with EOF inside parantheses?
How to run this bash command with EOF inside parantheses?

Time:07-21

I have this curl command:

curl -X POST "https://api.datadoghq.com/api/v1/synthetics/tests/trigger" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: xxx" \
-H "DD-APPLICATION-KEY: yyy" \
-d @- << EOF
{
  "tests": [
    {
      "public_id": "123-far-456"
    }
  ]
}
EOF

which I want to run inside () to get the result in to a variable. Like this:

curl_response=$(curl -X POST "https://api.datadoghq.com/api/v1/synthetics/tests/trigger" \
    -H "Content-Type: application/json" \
    -H "DD-API-KEY: xxx" \
    -H "DD-APPLICATION-KEY: yyy" \
    -d @- << EOF
    {
      "tests": [
        {
          "public_id": "123-far-456"
        }
      ]
    }
    EOF)

How can I escape EOF to run this? I tried -d @- << datadog.json but doesn't work.

CodePudding user response:

The token EOF has to be on a line of its own, and not start with any whitespace:

curl_response=$(curl -X POST "https://api.datadoghq.com/api/v1/synthetics/tests/trigger" \
    -H "Content-Type: application/json" \
    -H "DD-API-KEY: xxx" \
    -H "DD-APPLICATION-KEY: yyy" \
    -d @- << EOF
    {
      "tests": [
        {
          "public_id": "123-far-456"
        }
      ]
    }
EOF
)

Yeah, it's ugly. If you want pretty, don't use bash ;)

I tried -d @- << datadog.json but doesn't work.

When redirecting from a file, use single <. But in this case, curl allows you to do simply -d @datadog.json to read from the file without any shell magic.

  • Related