Escaping special characters in Spacelift tasks

Last updated: November 24, 2026

Resource addresses often contain double quotes and square brackets, for example module.example["resource_name"]. In a task, a shell processes your command before OpenTofu or Terraform runs it, and the shell removes unprotected double quotes. The command then fails or targets the wrong resource.

For example, tofu state show module.example["resource_name"] fails with:

Error: Error parsing instance address "module.example[resource_name]"

Where the shell processing happens:

  • Tasks in the Spacelift UI: the command runs in sh -c on the worker.

  • Tasks from spacectl: your local shell processes the command first, then sh -c processes it again on the worker.

The simplest fix: single quotes

Wrap the address in single quotes. The shell passes everything inside single quotes through unchanged:

tofu state show 'module.example["resource_name"]'

This also protects square brackets, which the shell can otherwise treat as wildcards.

If you can't use single quotes

Put a backslash before each double quote:

tofu state show module.example[\"resource_name\"]

Use one backslash, not more. Extra backslashes reach OpenTofu as part of the address.

With spacectl

The command passes through two shells, so put the whole task command in double quotes, and the address in single quotes inside it. Escape the inner double quotes:

bash

spacectl stack task --id <stack-id> "tofu state show 'module.example[\"resource_name\"]'"

Your local shell turns this into tofu state show 'module.example["resource_name"]', and the worker's shell then passes module.example["resource_name"] to OpenTofu.

Keeping the whole command in one quoted argument also stops spacectl from reading flags in it, such as -target, as its own.

These examples are for bash and zsh. PowerShell uses different quoting rules.

Check before you run

For state commands, run tofu state list in a task first, and copy the exact address from its output.

Sources