Skip to content

Read the Terraform version

Print the version from terraform --version, or exit if Terraform is missing.

18th August 2026

get_terraform_version() {
  local terraform_version

  command=$(command -v "terraform")
  if [[ -z $command ]]; then
    echo "Terraform is not installed - Aborting"
    exit
  fi

  terraform_version=$(terraform --version | head -n 1)
  terraform_version=${terraform_version##* }
  terraform_version=${terraform_version#?}

  if [[ -z ${terraform_version} ]]; then
    echo "Could not determine terraform version - Aborting"
    exit
  fi

  echo "${terraform_version}"
}

Caveats

Expects the first line of terraform --version to end with a v-prefixed version.