Gcloud command line get default project_id

Solution 1:

The easiest way to do this is to use the --format flag on gcloud.

gcloud config list --format 'value(core.project)' 2>/dev/null

The --format flag is available on all commands and gives you full control over what is printed, and how it is formatted.

You can see this help page for full info: gcloud topic formats

Solution 2:

Thanks to comment from Tim Swast above, I was able to use export PROJECT_ID=$(gcloud config get-value project) to get the project ID.

Running gcloud config get-value project prints the following on screen:

Your active configuration is: [default]

my-project-id

You can also run gcloud config get-value project 2> /dev/null to print just the project ID and suppress other warnings/errors.

Solution 3:

With Google Cloud SDK 266.0.0 you can use following command:

gcloud config get-value project