Skip to main content
trimsuffix(str, suffix) returns str with the trailing suffix removed. If str does not end with suffix, it is returned unchanged.

Signature

trimsuffix(str string, suffix string) string

Example

Strip the trailing slash from a base URL variable so paths can be constructed consistently with a single slash separator:
variable "api_base_url" {
  description = "Base URL of the API (with or without trailing slash)"
  type        = string
  default     = "https://api.example.com/"
}

computed "api_url" {
  description = "Normalized API base URL without trailing slash"
  expression  = trimsuffix(var.api_base_url, "/")
}

task "health-check" {
  description = "Check the API health endpoint"
  commands    = ["curl -f ${computed.api_url}/health"]
}
https://api.example.com/ becomes https://api.example.com.

Notes

  • Only the suffix at the end is removed. If the string ends with the suffix more than once, only one occurrence is removed.
  • trimsuffix removes a literal string match, not individual characters. Use trim for character-set stripping.