GCP: gcloud csv format with no-heading for Bash parsing

If you are scripting a set of gcloud commands, there is a good chance that it is within a Bash script.  While some Bash loops can be driven by a single variable, sometimes you need access to multiple variables within the loop.

In this article I will show you can drive a Bash loop requiring multiple variables using csv formatting.

As an example, here is a simple loop that lists all the GCP VM instances in your project driven by the single ‘name’ column.

# list all VM instances in default format, shows defaults columns
gcloud compute instances list

# loop driven by single column result 'name'
for vm in $(gcloud compute instances list --format='value(name)'); do 
  echo "GCP VM instance: $vm"
done

But this same pattern is not possible if you need multiple columns from the results inside the loop (for example both name and zone).  This can be addressed by using gcloud’s csv format with ‘no-heading’.

# list all VM instances in csv, show name and zone
gcloud compute instances list --format="csv[no-heading](name,zone)"

# loop driven by lines of output
for line in $(gcloud compute instances list --format="csv[no-heading](name,zone)"); do
  name=$(echo "$line" | cut -d, -f1)
  zone=$(echo "$line" | cut -d, -f2)
  echo "GCP VM instance: $name in zone $zone"
done

REFERENCES

gcloud utility

google, filtering and fun with gcloud

google, scripting gcloud

google, reference topic formats

stackoverflow, specify gcloud csv format