How to run a long bash command in multiline using heredoc

I am trying to use the local-exec provisioner and I have a long command that I would like to make multiline for readability sakes but I am having trouble doing so

using an az cli command as an example, I currently am doing this:

provisioner "local-exec" {
    command = "az iot hub routing-endpoint create --endpoint-resource-group ${} --endpoint-name ${} --endpoint-subscription-id ${} --endpoint-type eventhub --hub-name ${} --auth-type identitybased --identity ${} --endpoint-uri ${} --entity-path ${}"
  }

But I would like to use something like this

provisioner "local-exec" {
    command = <<EOD
az iot hub routing-endpoint create 
--endpoint-resource-group ${} \
--endpoint-name ${} \
--endpoint-subscription-id ${} \
--endpoint-type eventhub \
--hub-name ${} \
--auth-type identitybased \
--identity ${} \
--endpoint-uri ${} \
--entity-path ${} \
EOD
  }

but it complains about the backslash and if i don’t use the backslash, it tries to run every new line as its own command

Has anyone gotten around this?

1 Like

Hi,
I had the same problem. Check the command “plan” output and if you got in the command the “\r\n”. Something like this

az iot hub routing-endpoint \\r\n

If so, change the encoding for the file to “linux line ending” and try again. That fixes the issue to me. Also remove all spaces after \

Kind regards,
Sergio

Thanks Sergio, you’ve saved me many hours.

For those who use VSCode, make sure you change your encoding from “CRLF” to “LF” (bottom right)