Skip to main content
pathexpand(path) replaces a leading ~ with the current user’s home directory. If path does not start with ~, it is returned unchanged.

Signature

pathexpand(path string) string

Example

Allow users to provide a path using ~ notation for their personal configuration or credential files:
variable "kubeconfig" {
  description = "Path to the kubeconfig file"
  type        = string
  default     = "~/.kube/config"
}

computed "kubeconfig_abs" {
  description = "Expanded kubeconfig path"
  expression  = pathexpand(var.kubeconfig)
}

task "deploy" {
  description = "Deploy to the cluster"
  commands    = ["kubectl --kubeconfig ${computed.kubeconfig_abs} apply -f k8s/"]
}
~/.kube/config becomes /home/user/.kube/config (or the platform equivalent).

Notes

  • Only a leading ~ is expanded. ~/some/path is supported. some~/path and ~user/path are not.
  • pathexpand reads the user’s home directory from the environment. On most systems this is $HOME on Linux and macOS, and %USERPROFILE% on Windows.