bash auto-completion on Linux
The kubectl completion script for Bash can be generated with the command . 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 (you can test if you have bash-completion already installed by running type _init_completion
).
bash-completion is provided by many package managers (see ). You can install it with apt-get install bash-completion
or yum install bash-completion
, etc.
To find out, reload your shell and run type _init_completion
. If the command succeeds, you’re already set, otherwise add the following to your ~/.bashrc
file:
Reload your shell and verify that bash-completion is correctly installed by typing .
You now need to ensure that the kubectl completion script gets sourced in all your shell sessions. There are two ways in which you can do this:
Add the completion script to the
/etc/bash_completion.d
directory:
If you have an alias for kubectl, you can extend shell completion to work with that alias:
Last modified March 05, 2021 at 3:04 AM PST : Update install-kubectl.md to use tabs for the different Operating Systems (#26682) (87e3291aa)