76

I’m looking for a gcloud one-liner to get the default project ID ($GCP_PROJECT_ID).

The list command gives me:

gcloud config list core/project

#=>

[core]
project = $GCP_PROJECT_ID

Your active configuration is: [default]

While I only want the following output:

gcloud . . .

#=>

$GCP_PROJECT_ID
Mike
  • 1,080
  • 1
  • 9
  • 25
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56

6 Answers6

72

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 the get-value command prints the following:

gcloud config get-value project


#=>

Your active configuration is: [default]

$PROJECT_ID

You can also run:

gcloud config get-value project 2> /dev/null

to just print $PROJECT_ID and suppress other warnings/errors.

Mike
  • 1,080
  • 1
  • 9
  • 25
Nikhil Jindal
  • 1,053
  • 9
  • 10
  • 1
    You can also run `gcloud config get-value project 2> /dev/null` to print just the project ID and suppress other warnings/errors. Updated the answer. – Nikhil Jindal Nov 28 '17 at 01:44
  • This is the best way I've found too, but it's quite slow because `gcloud` is just slow to start up. 2-3 sec per call, so (for instance) too slow to put in a shell prompt function. Is there any way that's faster? – GaryO Oct 23 '20 at 14:52
  • This is the solution I used too – Zaffer Dec 26 '22 at 20:56
70

The easiest way to do this is to use the --format flag with 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
Mike
  • 1,080
  • 1
  • 9
  • 25
Mark
  • 2,265
  • 15
  • 13
  • 3
    Note that this prints some extra information to standard error, but the actual value is printed to standard out. I was able to capture the project ID into an environment variable with `export PROJECT_ID=$(gcloud config list --format 'value(core.project)')` – Tim Swast Oct 14 '16 at 16:37
  • It doesn't print anything to stderr for me - maybe that's changed? Google Cloud SDK version 227 – akauppi Dec 09 '18 at 10:13
36

With Google Cloud SDK 266.0.0 you can use following command:

gcloud config get-value project
Oldrich Dlouhy
  • 597
  • 6
  • 6
6

From Cloud Shell or any machine where Cloud SDK is installed, we can use:

echo $DEVSHELL_PROJECT_ID

And as shown in the below screenshot.

A command to get GCP project ID

I got a question about how to set the environment variable $DEVSHELL_PROJECT_ID; here are the steps:

  • If the URL has the variable project and is set to some project id, then the environment variable $DEVSHELL_PROJECT_ID usually will be set to the project id.
  • If the variable project is not set in the URL, we can choose the project from the Combobox (besides the title Google Cloud Platform) which will set the variable project in the URL. We may need to restart the Cloud Shell or refresh the entire web page to set the environment variable $DEVSHELL_PROJECT_ID.
  • Otherwise, if the environment variable $DEVSHELL_PROJECT_ID is not set, we can set it by the command shown below where we replace PROJECT_ID with the actual project id.
gcloud config set project PROJECT_ID
  • All these are shown in the below figure.

How to set the environment variable $DEVSHELL_PROJECT_ID

Fady Ibrahim
  • 376
  • 4
  • 10
  • The environment variable `$DEVSHELL_PROJECT_ID` does **not** _seem_ to be set by default on a machine with the Cloud SDK installed. Would you be able to update this answer with some supporting information about `$DEVSHELL_PROJECT_ID`? – Mike May 13 '22 at 22:12
  • 1
    Hello @Mike, I have updated the answer. I hope this may help. – Fady Ibrahim May 16 '22 at 12:34
5

Not exactly the gcloud command you specified, but will return you the currently configured project:

gcloud info |tr -d '[]' | awk '/project:/ {print $2}'

Works for account, zone and region as well.

mssch
  • 154
  • 7
1

Direct and easy way to get the default $PROJECT_ID is answered above.

In case you would like to get $PROJECT_ID from the info command, here is a way to do it:

gcloud info --format=flattened | awk '/config.project/ {print $2}'

or:

gcloud info --format=json | jq '.config.project' | tr -d '"'

Just run:

gcloud info --format={flattened|json}

to see the output, then use awk, jq or similar tools to grab what you need.

Mike
  • 1,080
  • 1
  • 9
  • 25
Robert Ranjan
  • 1,538
  • 3
  • 19
  • 17