Skip to main content
trimspace(str) removes all leading and trailing whitespace characters from str, including spaces, tabs, and newlines.

Signature

trimspace(str string) string

Example

Read a project name from a configuration file and strip any accidental whitespace before using it in commands:
computed "project" {
  description = "Project name from the config file"
  expression  = trimspace(file(".project"))
}

task "build" {
  description = "Build the Docker image with the project name"
  commands = [
    "docker build -t ${computed.project}:latest .",
  ]
}
If .project contains myapp\n, computed.project becomes myapp.

Notes

  • trimspace removes whitespace from both ends. It does not affect whitespace in the middle of the string.
  • To remove only trailing newlines, use chomp.
  • To remove a specific set of characters, use trim.