Skip to main content

Configuring bash-completion for kubectl

kube_logo

First make sure you have bash-completion installed on your computer.

Installing bash-completion

Depending on the linux distribution you are using the way to install will vary but the package name is generally the same, here are examples:

Ubuntu

apt-get install bash-completion

Ret-hat, Cent-OS, Rocky

yum install bash-completion

These commands create /usr/share/bash-completion/bash_completion, which takes care of command line completion. Depending on your distribution you will have a file called ~/.bashrc.

To make sure, reload your shell using type _init_completion. If it succeeds, it is already configured; if not add the following to your ~/.bashrc file

source /usr/share/bash-completion/bash_completion

And try reloading your Shell

Enable kubectl autocompletion

To make sure that autocompletion is available in all sessions you can do the following:

  • Enable it in your profile:
echo 'source <(kubectl completion bash)' >>~/.bashrc
  • Add it to the general /etc/bash_completion.d directory:
kubectl completion bash >/etc/bash_completion.d/kubectlkubectl completion bash >/etc/bash_completion.d/kubectl

Even if you have an alias you can also configure it:

echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc