Terraform output cannot produce a single backslash - escape character behavior is unexpected

I’m trying to make a file system object, export and mount point that will be accessible to windows servers with a UNC path.
Everything is working fine, except that I want to output a UNC path but can’t get the slash numbers right.

my output:

output "UNC_Path" {
    value = "\\\\${oci_file_storage_mount_target.UNC_MountTarget.ip_address}\\${trim("${oci_file_storage_export.UNC_export.path}","//")}\\"

expected result:

  "UNC_Path" = "\\\UNC\"


  "UNC_Path" = "\\\\\\UNC\\"

How do I get this output to give me a single backslash? it seems to read as literal double backslashes, but errors out if I use single backslashes. So I can neither put in literally what I want, NOR use the backlash as an escape character to a literal backslash. I can’t understand what terraform wants me to do here.

Hi @jmdoman,

I think you are confusing the escaped format with the actual data. The output you show is a quoted string, therefore must have the \ character escaped. The value you are showing contains exactly the string you want, in other words the quoted string "\\\\\\UNC\\" contains the data \\\UNC\.

1 Like

Thanks jbardin, so in other words if I wanted to feed this output to, for example, a text file, it would result in the single slash version? I am new to terraform and am prototyping right now.

That depends on how you are inserting this into a text file, and what format that text file expects. Perhaps it would be more clear if we look at structured data rather than the human oriented output.

% tf show -json | jq .values.outputs.UNC_Path.value

The json string is going to have the same format, because it must be quoted. If you want the raw data, there is also the -raw flag for the output command which works for primitive values.