Skip to main content
dirname(path) returns all but the last element of path, effectively returning the parent directory.

Signature

dirname(path string) string

Example

Derive the working directory for a task from a variable that points to a specific binary or config file:
variable "config_file" {
  description = "Path to the service configuration file"
  type        = string
  default     = "services/api/config.yaml"
}

computed "service_dir" {
  description = "Directory containing the service config"
  expression  = dirname(var.config_file)
}

task "validate-config" {
  description = "Validate the service configuration"
  working_dir = computed.service_dir
  commands    = ["./validate config.yaml"]
}
services/api/config.yaml becomes services/api.

Notes

  • dirname does not check whether the path exists.
  • If path has no directory component (e.g., just a filename), dirname returns ".".
  • To get just the filename, use basename.