bash auto-completion on macOS
The kubectl completion script for Bash can be generated with . Sourcing this script in your shell enables kubectl completion.
However, the kubectl completion script depends on bash-completion which you thus have to previously install.
The instructions here assume you use Bash 4.1+. You can check your Bash’s version by running:
If it is too old, you can install/upgrade it using Homebrew:
brew install bash
Homebrew usually installs it at /usr/local/bin/bash
.
You can test if you have bash-completion v2 already installed with type _init_completion
. If not, you can install it with Homebrew:
As stated in the output of this command, add the following to your ~/.bash_profile
file:
Reload your shell and verify that bash-completion v2 is correctly installed with type _init_completion
.
Source the completion script in your
~/.bash_profile
file:Add the completion script to the
/usr/local/etc/bash_completion.d
directory:If you have an alias for kubectl, you can extend shell completion to work with that alias:
echo 'alias k=kubectl' >>~/.bash_profile
If you installed kubectl with Homebrew (as explained ), then the kubectl completion script should already be in . In that case, you don’t need to do anything.
Last modified March 15, 2021 at 11:14 AM PST : Fix broken link (bf312020c)