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

Viewed 3462

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
1 Answers

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.

Related