Skip to content

Instantly share code, notes, and snippets.

@dwchiang
Created April 16, 2014 10:30
Show Gist options
  • Star 78 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dwchiang/10849350 to your computer and use it in GitHub Desktop.
Save dwchiang/10849350 to your computer and use it in GitHub Desktop.
gcloud with zsh
# The next line updates PATH for the Google Cloud SDK.
source /Users/dwchiang/google-cloud-sdk/path.zsh.inc
# The next line enables zsh completion for gcloud.
source /Users/dwchiang/google-cloud-sdk/completion.zsh.inc
script_link="$( readlink "$0" )" || script_link="$0"
apparent_sdk_dir="${script_link%/*}"
if [ "$apparent_sdk_dir" == "$script_link" ]; then
apparent_sdk_dir=.
fi
sdk_dir="$( cd -P "$apparent_sdk_dir" && pwd -P )"
bin_path="$sdk_dir/bin"
export PATH=$bin_path:$PATH
@seguelador
Copy link

Finally I've got the solution. Thanks a lot

@MOZGIII
Copy link

MOZGIII commented Mar 12, 2019

For linux (ubuntu in particular) that'd be source /usr/share/google-cloud-sdk/completion.zsh.inc

@rafitadiaz
Copy link

Or if you install from snap: source /snap/google-cloud-sdk/current/completion.zsh.inc

@zycon
Copy link

zycon commented Aug 21, 2019

Wahh..

@Jleagle
Copy link

Jleagle commented Oct 3, 2019

Running gcloud/install.sh will add this to the correct rc file automatically.

@biniama
Copy link

biniama commented Oct 16, 2019

Also see brew cask info that tell to source those files in the zshrc.

 ➜ brew cask info google-cloud-sdk
google-cloud-sdk: latest
https://cloud.google.com/sdk/
/usr/local/Caskroom/google-cloud-sdk/latest (15,579 files, 201.8MB)
From: https://github.com/caskroom/homebrew-cask/blob/master/Casks/google-cloud-sdk.rb
==> Name
Google Cloud SDK
==> Artifacts
google-cloud-sdk/install.sh (Installer)
google-cloud-sdk/bin/bq (Binary)
google-cloud-sdk/bin/gcloud (Binary)
google-cloud-sdk/bin/git-credential-gcloud.sh -> git-credential-gcloud (Binary)
google-cloud-sdk/bin/gsutil (Binary)
==> Caveats
google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile:

  for bash users
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc'
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc'

  for zsh users
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc'
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'

Thanks. This worked for me!

@mmalovichko
Copy link

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

@rahmanya
Copy link

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

This worked for me as well, was able to also run gsutil commands after updating zsh using source ~/.zshrc

@mgruesbeck
Copy link

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

This was the easiest for me in 2020.

@Enterprise-Architecture

Thank you for sharing it , it worked for me.

@Silverium
Copy link

and still working on March 2020 :D

@s4l4x
Copy link

s4l4x commented Apr 21, 2020

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

This

@appsparkler
Copy link

Great! Thanks for this 👍 💯 😄

@clarkmcc
Copy link

Thank you!

@Ricky-NSW
Copy link

I was getting an error on line 3 : "/Users//google-cloud-sdk/path.zsh.inc:3 file not found"

Echoed script_link and apparent_sdk_dir and were the same and correct.

This worked for me when I added double brackets to line 3 in path.zsh.inc:

script_link="$( readlink "$0" )" || script_link="$0" apparent_sdk_dir="${script_link%/*}" if [[ "$apparent_sdk_dir" == "$script_link" ]]; then apparent_sdk_dir=. fi sdk_dir="$( cd -P "$apparent_sdk_dir" && pwd -P )" bin_path="$sdk_dir/bin" export PATH=$bin_path:$PATH

Mac OSX 10.15.5
oh-my-zsh installed too

@DaveClissold
Copy link

DaveClissold commented Jun 16, 2020

@Ricky-NSW Thanks, was thinking I was going mad but the brackets on line 3 resolved it for me! But you get a parsing error around the if statement, if you don't format it correctly,

script_link="$( readlink "$0" )" || script_link="$0" 
apparent_sdk_dir="${script_link%/*}" 
if [[ "$apparent_sdk_dir" == "$script_link" ]]; then
  apparent_sdk_dir=. 
fi
sdk_dir="$( cd -P "$apparent_sdk_dir" && pwd -P )" 
bin_path="$sdk_dir/bin" 
export PATH=$bin_path:$PATH

@Mirra1602
Copy link

@Ricky-NSW Thank you for sharing! I had the same error.

@akhiljalagam
Copy link

source /snap/google-cloud-sdk/current/completion.zsh.inc

thank you. 😃

@mustafaergul
Copy link

Thanks for this !

@eepaul
Copy link

eepaul commented Dec 2, 2020

@Ricky-NSW Thanks, was thinking I was going mad but the brackets on line 3 resolved it for me! But you get a parsing error around the if statement, if you don't format it correctly,

script_link="$( readlink "$0" )" || script_link="$0" 
apparent_sdk_dir="${script_link%/*}" 
if [[ "$apparent_sdk_dir" == "$script_link" ]]; then

I was in the same situation. I am using if [ "$apparent_sdk_dir" = "$script_link" ]; then

@gopinathppm
Copy link

great. this fixed the issue

@seeess1
Copy link

seeess1 commented Apr 6, 2021

Should I answer yes or no to the Ignore insecure directories and continue message and then run the source commands listed at the top of this thread? I've downloaded the SDK package, unzipped it in my home directory, and I ran ./google-cloud-sdk/install.sh. But now that I've opened a new terminal, I'm getting that warning message and I'm not sure what to do next in order to follow along with the recommendations posted here.

@seeess1
Copy link

seeess1 commented Apr 9, 2021

Update: I reinstalled zsh on my machine and that took care of the issue.

@talesmarrads
Copy link

This works!! thanks for that! 👏

@talesmarrads
Copy link

Btw for those encountering this issue that contains this message: Ignore insecure directories and continue

This can help https://stackoverflow.com/questions/13762280/zsh-compinit-insecure-directories

@jonolayton
Copy link

Thanks very much - fixed a super annoying issue.

@raphaelzenou
Copy link

raphaelzenou commented Sep 7, 2021

Also see brew cask info that tell to source those files in the zshrc.

 ➜ brew cask info google-cloud-sdk
google-cloud-sdk: latest
https://cloud.google.com/sdk/
/usr/local/Caskroom/google-cloud-sdk/latest (15,579 files, 201.8MB)
From: https://github.com/caskroom/homebrew-cask/blob/master/Casks/google-cloud-sdk.rb
==> Name
Google Cloud SDK
==> Artifacts
google-cloud-sdk/install.sh (Installer)
google-cloud-sdk/bin/bq (Binary)
google-cloud-sdk/bin/gcloud (Binary)
google-cloud-sdk/bin/git-credential-gcloud.sh -> git-credential-gcloud (Binary)
google-cloud-sdk/bin/gsutil (Binary)
==> Caveats
google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile:

  for bash users
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc'
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc'

  for zsh users
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc'
    source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'

Fantastic, thanks a lot for your help.

Just a quick note that I had to use brew info google-cloud-sdk and not brew cask info google-cloud-sdk with Homebrew's version 3.2.11 as the cask command seems now deprecated.

@khouryap
Copy link

khouryap commented Nov 2, 2021

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

On a Mac and this was the simple solution for me after a brew install google-cloud-sdk

@louicoder
Copy link

Adding gcloud to the plugin list in ~/.zshrc did the trick for me

This worked for me in March 2022, probably the easiest solution on macOS

@ryaminal
Copy link

ryaminal commented Apr 3, 2023

@louicoder i'm assuming that when you, and others, are referring to "adding gcloud to the plugin list` you are using oh-my-zsh and this is the oh-my-zshell plugin list and corresponding plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment