How to have kubectl auto-completion in MacOS X?

If you try to follow the steps in the k8s docs for a MaOS environment you will see that it will not work as expected. Then, follow the steps in order to make it works well.

  • Install kubectl, if you do not have it already

$ brew install kubectl

  • Install the latest version of bash and bash-completion.

$ brew install bash

# It will not work with versions < 2

$ brew install bash-completion@2

  • Then, add the path /usr/local/bin/bash in the  /etc/shells

$ sudo echo “/usr/local/bin/bash” > /etc/shells

  • Set to the installed shell to the current user.

chsh -s /usr/local/bin/bash

  • Run:

kubectl completion bash > ~/.kube/kubectl_autocompletion

  • Add the following content in the bash_profile (vim ~/.bash_proofile)

# kubectl autocompletion
if [ -f /usr/local/share/bash-completion/bash_completion ]; then
. /usr/local/share/bash-completion/bash_completion
fi

source ~/.kube/kubectl_autocompletion

Now, after restarting the terminal you will be able to use the kubectl autocomplete with the TAB button.  Following an image as an example.

Screenshot 2019-10-20 at 00.57.54

Sponsored by:

Logo-horizontalCP Logo Big - vertical invert (1)

Leave a comment