Turn list into literal string with quotes

Hello,

I’m trying to turn a list into its literal correspondent, so basically the same thing without the square brackets:

["a", "b", "c"]

should be turned into:

"a", "b", "c"

There’s no use case here, but I’d like to understand better how terraform works in these cases.
What I’ve tried is:

> "${join("\", \"", ["a", "b", "c"])}"
"a\", \"b\", \"c"

As you can see, the slashes themselves are output too. This is rather weird to me, because on the one hand, they do work as escaping characters (without them, it would error out), and on the other hand, they’re also output. Any ideas what’s happening here?

I’ve just realised that I’m making a confusion, I’m mixing up the external quotes, which are conventional, and are not part of the actual output, with the internal ones, where I’m guessing the backslashes are output so as to tell them apart from the external ones.
I guess join is not enough for this to work.

You appear to be using terraform console.

Output from this is not raw text, but correctly formatted Terraform expressions - including quoting/escaping where needed.

1 Like

Ok, I’ve realised I’ve done this before with your help.

The gist of it is:

resource "local_file" "crap" {
        filename = "crap.txt"
        content = join(", ", [for name in var.crap : format("%q", name)])
}

And crap is:

crap = ["one", "two"]

If you include a newline character within a string, terraform console will render it in here-doc format, which may aid you in visualizing the output:

> join("\", \"", ["a", "b", "c\n"])
<<EOT
a", "b", "c

EOT
1 Like