9879ypxkj
9879ypxkj

Reputation: 437

What does the "%%bash" command do in python?

This is from a Python notebook on the google cloud platform guides.

Can you please explain to me how to interpret and understand this code?

%%bash
export PROJECT=$(gcloud config list project --format "value(core.project)")
echo "Your current GCP Project Name is: "$PROJECT

It gives the following output:

Your current GCP Project Name is: cpb123-123123

Upvotes: 5

Views: 9014

Answers (1)

Dominik Érsek
Dominik Érsek

Reputation: 116

GNU Bash or simply Bash is a Unix shell and command language.

%%bash

Means, that the following code will be executed by bash.

In bash $() means it will return with the result of the commands inside the parentheses, in this case the commands are:

gcloud config list project --format "value(core.project)"

Google cloud has its own command set to control your projects. This command will give you the core project you are working on.

export PROJECT=$(gcloud config list project --format "value(core.project)")
echo "Your current GCP Project Name is: "$PROJECT

The export command in bash is used to set values to environmental variables. In the case it basically sets the value of an environmental variable called PROJECT and the echo just echoes the value back to the console.

You can find more on bash here.

Upvotes: 10

Related Questions