Skip to main content
chomp(str) removes trailing newline characters (\n, \r\n, and \r) from the end of a string. It does not affect newlines in the middle of the string.

Signature

chomp(str string) string

Example

Read a version file and strip the trailing newline before using it in a command:
computed "version" {
  description = "Application version from the VERSION file"
  expression  = chomp(file("VERSION"))
}

task "tag" {
  description = "Tag the current commit with the version"
  commands = [
    "git tag v${computed.version}",
    "git push origin v${computed.version}",
  ]
}
file("VERSION") reads the file contents including any trailing newline. chomp removes it so the version is clean for use in the git tag.

Notes

  • chomp removes only trailing newlines, not spaces or tabs. Use trimspace to strip all whitespace.
  • If the string does not end with a newline, it is returned unchanged.