Thanks to comment from Tim Swast above, I was able to use:
多亏了Tim SWAST上面的评论,我才能使用:
export PROJECT_ID=$(gcloud config get-value project)
to get the project ID. Running the get-value
command prints the following:
来获取项目ID。运行Get-Value命令将打印以下内容:
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.
仅打印$PROJECT_ID并取消其他警告/错误。
The easiest way to do this is to use the --format
flag with gcloud
:
要做到这一点,最简单的方法是对gCloud使用--Format标志:
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.
--Format标志在所有命令上都可用,并使您可以完全控制打印的内容以及如何格式化。
You can see this help page for full info:
有关完整信息,您可以查看此帮助页面:
gcloud topic formats
With Google Cloud SDK 266.0.0 you can use following command:
在Google Cloud SDK 266.0.0中,您可以使用以下命令:
gcloud config get-value project
From Cloud Shell or any machine where Cloud SDK is installed, we can use:
在Cloud Shell或任何安装了Cloud SDK的机器上,我们可以使用:
echo $DEVSHELL_PROJECT_ID
And as shown in the below screenshot.
如下面的截图所示。
I got a question about how to set the environment variable $DEVSHELL_PROJECT_ID; here are the steps:
我遇到了一个关于如何设置环境变量$DEVSHELL_PROJECT_ID的问题;以下是步骤:
- 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.
Not exactly the gcloud command you specified, but will return you the currently configured project:
不完全是您指定的gCloud命令,但会返回当前配置的项目:
gcloud info |tr -d '[]' | awk '/project:/ {print $2}'
Works for account, zone and region as well.
也适用于帐户、区域和区域。
Direct and easy way to get the default $PROJECT_ID
is answered above.
上面回答了获取默认$PROJECT_ID的直接而简单的方法。
In case you would like to get $PROJECT_ID
from the info
command, here is a way to do it:
如果您想要从INFO命令获取$PROJECT_ID,以下是一种方法:
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.
要查看输出,请使用awk、jq或类似工具获取所需内容。
更多回答
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.
您还可以运行gCloud配置Get-Value Project 2>/dev/null来仅打印项目ID并抑制其他警告/错误。更新了答案。
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?
这也是我发现的最好的方法,但它相当慢,因为gCloud启动起来很慢。每次调用2-3秒,所以(例如)太慢了,无法放入外壳提示功能。有没有更快的方法?
This is the solution I used too
这也是我用过的解决方案
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)')
请注意,这会将一些额外信息打印到标准错误,但实际值会打印到标准输出。我能够使用EXPORT PROJECT_ID=$(gCloud配置列表--格式‘Value(core.project)’)将项目ID捕获到环境变量中。
It doesn't print anything to stderr for me - maybe that's changed? Google Cloud SDK version 227
它没有为我打印任何标准的东西--也许这已经改变了?Google Cloud SDK版本227
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
?
在安装了Cloud SDK的计算机上,似乎没有默认设置环境变量$DEVSHELL_PROJECT_ID。您是否可以使用有关$DEVSHELL_PROJECT_ID的一些支持信息来更新此答案?
Hello @Mike, I have updated the answer. I hope this may help.
您好,@Mike,我已经更新了答案。我希望这能有所帮助。
我是一名优秀的程序员,十分优秀!