String interpolation and escaping
Build a resource name from two inputs
A string holds text. Interpolation lets an expression supply part of that text. A team can use one naming pattern for several environments instead of editing every name manually. In a fresh local configuration, use:
variable "service" { type = string default = "catalog"}
variable "environment" { type = string default = "dev"}
output "name" { value = "${var.service}-${var.environment}"}The predicted output is catalog-dev. Changing environment to test gives catalog-test. Terraform evaluates the expressions inside the dollar-and-brace markers, then combines their text with the hyphen. This output is only a name suggestion; no resource is created and provider naming restrictions are not checked.
Write quoted strings and escapes
Double quotes delimit ordinary Terraform string literals. Backslash escapes let a string contain characters that would otherwise end the literal or be difficult to type. These are separate console expressions, with predicted results in comments.
"first\nsecond" # two lines"label: \"catalog\"" # label: "catalog""folder\\report.txt" # folder\report.txt"caf\u00e9" # café"a\tb" # a, tab, b"a\rb" # a, carriage return, b"\U0001F680" # one rocket characterUnicode escapes use four hexadecimal digits with \u or eight with \U. Keep literal strings in UTF-8. A tab or carriage return may display differently in terminals; inspect escaped output when exact characters matter. Single quotes are not an alternative delimiter for Terraform strings.
Keep template markers literal
Sometimes the resulting text must contain a marker intended for a different system. Use doubled dollar or percent signs at the opening marker to prevent Terraform from interpreting it.
"$${name}" # ${name}"%%{ if ready }" # %{ if ready }"port-${8080}" # port-8080Do not escape interpolation with a backslash. A normal numeric expression can become text inside a larger string, but a collection or null cannot simply be interpolated as ordinary text. If you only need the original value, write the reference directly rather than adding unnecessary interpolation. For a structured text representation, use an encoder in the next lesson.
Use multiline text without accidental indentation
A heredoc is a string spread over several source lines. Select a delimiter that does not occur alone inside the content. The indented form lets source code stay readable while removing common leading spaces.
output "report" { value = <<-REPORT Inventory catalog REPORT}The predicted text is Inventory\n catalog\n when displayed with newline escapes. The second line retains two spaces and the final newline remains. The ordinary <<REPORT form instead preserves the indentation of the content lines and needs an unindented closing delimiter. In either form, backslashes are ordinary characters, while interpolation and directives still work. Do not put secret material into a heredoc merely because it looks like a separate file.
Check the text before using it
Common mistakes include confusing a source-code escape with an actual output character, forgetting that a heredoc has a final newline, or trying to interpolate a null value. A known null is not an empty string. Choose a fallback intentionally.
variable "suffix" { type = string default = null}
output "label" { value = "catalog-${var.suffix == null ? "dev" : var.suffix}"}The default predicts catalog-dev; an explicitly empty suffix predicts catalog-, which may not be a valid provider name. This is a separate configuration. Practice: predict two names, literal markers and the exact multiline text before moving to templates. All examples are static predictions; no Terraform execution has been performed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
String interpolation and escaping — knowledge check
Check text rendering and encoding behavior.
What does the default naming configuration return?
Which quoted string yields literal ${name}?
What does \n represent inside a quoted string?
What is different about the <<- heredoc form?
What is the guarded suffix example output for a null input?