The kubectl completion script for Bash can be generated with the command kubectl completion bash
. Sourcing the completion script in your shell enables kubectl autocompletion. However, the completion script depends on bash-completion, which means that you have to install this software first.
Install and enable bash-completion
You can test if you have bash-completion already installed by running type _init_completion
. If it is not present, you can install it with your package manager (i.e. apt-get install bash-completion
or dnf install bash-completion
). The installation creates /usr/share/bash-completion/bash_completion
, which is the main script of bash-completion. You now need to ensure that the kubectl completion script gets sourced in all your shell sessions. Insert the following lines into the .bashrc
file
# User specific aliases and functions source /usr/share/bash-completion/bash_completion source <(kubectl completion bash) # If you have an alias for kubectl, you can extend shell completion to work with that alias alias k=kubectl complete -F __start_kubectl k
After reloading your shell, kubectl
autocompletion should be working.