{"record":{"id":"315409cf0eafc4d6","repo":"hashicorp/packer","slug":"error-interpolating-inline-s","errorCode":null,"errorMessage":"Error interpolating Inline: %s","messagePattern":"Error interpolating Inline: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/shell/provisioner.go","lineNumber":217,"sourceCode":"\t// If we have an inline script, then turn that into a temporary\n\t// shell script and use that.\n\tif p.config.Inline != nil {\n\t\ttf, err := tmp.File(\"packer-shell\")\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error preparing shell script: %s\", err)\n\t\t}\n\t\tdefer os.Remove(tf.Name())\n\n\t\t// Set the path to the temporary file\n\t\tscripts = append(scripts, tf.Name())\n\n\t\t// Write all inline commands to this buffer\n\t\tcommandBuffer := strings.Builder{}\n\t\tfor _, command := range p.config.Inline {\n\t\t\tp.config.ctx.Data = generatedData\n\t\t\tcommand, err := interpolate.Render(command, &p.config.ctx)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"Error interpolating Inline: %s\", err)\n\t\t\t}\n\t\t\tif _, err := fmt.Fprintf(&commandBuffer, \"%s\\n\", command); err != nil {\n\t\t\t\treturn fmt.Errorf(\"Error preparing shell script: %s\", err)\n\t\t\t}\n\t\t}\n\n\t\t// If the user has defined an inline shebang, use that.\n\t\t// Or If command does not start with a shebang, use the default shebang.\n\t\t// else command already has a shebang, so do not write it.\n\t\tif p.config.inlineShebangDefined || !strings.HasPrefix(commandBuffer.String(), \"#!\") {\n\t\t\tif _, err := fmt.Fprintf(tf, \"#!%s\\n\", p.config.InlineShebang); err != nil {\n\t\t\t\treturn fmt.Errorf(\"Error preparing shell script: %s\", err)\n\t\t\t}\n\t\t}\n\n\t\t// Write the collected commands to the file\n\t\tif _, err := tf.WriteString(commandBuffer.String()); err != nil {\n\t\t\treturn fmt.Errorf(\"Error preparing shell script: %s\", err)","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/shell/provisioner.go#L199-L235","documentation":"This error means Packer could not render one of the shell provisioner's `inline` command strings through the HCL2/template interpolation engine. interpolate.Render evaluates variables, functions, and user variables in the command text; a failure here (bad function call, unknown variable, wrong argument types) aborts the whole Provision run before any script file is written. The original interpolation error is appended to the message.","triggerScenarios":"Calling the shell provisioner's Provision with an `inline` entry containing invalid template syntax: an undefined `var.foo`, a misused template function like `${lower(...)}` instead of `lower(...)`, unbalanced `${`, or a function returning an error for the given generatedData (e.g. referencing a build-time key that does not exist).","commonSituations":"Typing `${build.ID}` when the data source key is actually `build.Name`; forgetting to define a variable in `variables`/`locals`; copy-pasting shell `${VAR}` expansion into HCL where Packer tries to interpret it; upgrading Packer and a deprecated template function disappearing.","solutions":["Fix the template syntax reported by the wrapped error: quote it as a literal by escaping `$` as `$${...}` when you want shell expansion, not Packer interpolation","Define or correct the referenced variable/local so `var.x`, `local.y`, or `build.z` exists at provision time","Validate the template with `packer init` + `packer validate` before running the build","If the value comes from generatedData (e.g. build), confirm the key is produced by an earlier builder/data source in the same build"],"exampleFix":"// before\ninline = [\"echo ${undefined_var}\"]\n// after\ninline = [\"echo ${var.my_var}\"]  // or escape shell syntax: \"echo $${SOME_SHELL_VAR}\"","handlingStrategy":"validation","validationCode":"# before running the build\npacker validate template.pkr.hcl\n# also escape shell expansions: $${VAR} instead of ${VAR} inside inline","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `packer validate` in CI before every build","Prefer $${...} escaping for shell variable expansion inside HCL inline strings","Keep all var./local./build. references defined before the provisioner executes","Break long inline strings into locals so interpolation errors are easier to localize"],"tags":["go","packer","template-interpolation","shell-provisioner"],"backgroundTag":"template-interpolation-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}