Jordi
Jordi

Reputation: 23187

Shell script: variables inside curl body

I need to interpolate curl body with variables into a shell script:

This is the script snippet:

    curl -k \
      -X PUT \
      -d @- \
      -H "Authorization: Bearer $TOKEN" \
      -H "Accept: application/json" \
      -H "Content-Type: application/json" \
      "$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<-'EOF'
      {
        "kind": "Secret",
        "apiVersion": "v1",
        "metadata": {
          "name": "$SECRET_ID"
        },
        "stringData": {
          "$key": "$value"
        }
      }
EOF

However, neither $SECRET_ID nor $key nor $value aure "resolved".

EDIT

I've tried that:

    curl -k \
      -X PUT \
      -d @- \
      -H "Authorization: Bearer $TOKEN" \
      -H "Accept: application/json" \
      -H "Content-Type: application/json" \
      "$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<-"EOF"
      {
        "kind": "Secret",
        "apiVersion": "v1",
        "metadata": {
          "name": "$SECRET_ID"
        },
        "stringData": {
          "$key": "$value"
        }
      }
EOF

But it doesn't works.

Upvotes: 0

Views: 56

Answers (1)

Amadan
Amadan

Reputation: 198294

You quoted your JSON in heredoc with <<-'EOF'. If the heredoc delimiter is quoted, it will not expand variables. Use <<-EOF to get the normal double-quoted variable-expansiony semantics.

Upvotes: 2

Related Questions