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.
Step 1: Please check the following command to see that the Linux package is installed.
$ type _init_completion
Step 2: If you did not install the bash-completion, you can install bash-completion
by:
$ sudo apt-get install bash-completion
Step 3: The above commands create /usr/share/bash-completion/bash_completion
, which is the main script of bash-completion. Depending on your package manager, you have to manually source this file in your ~/.bashrc
file.
$ source /usr/share/bash-completion/bash_completion
Step 4: Reload your shell and check the package is successfully installed. If you cannot get the content, then please check step 3 is successfully configured.
type _init_completion
You now need to ensure that the kubectl completion script gets sourced in all your shell sessions.
Note: bash-completion sources all completion scripts in
/etc/bash_completion.d
.
echo 'source <(kubectl completion bash)' >>~/.bashrc
If you have an alias for kubectl, you can extend shell completion to work with that alias:
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc
After reloading your shell, kubectl autocompletion should be working.
Official Document - bash auto-completion on Linux:
Github - bash-completion:
GitHub - scop/bash-completion: Programmable completion functions for bash