Why doesn't kubectl bash completion work on macOS/OS X?

7/21/2017

I followed the instructions for installing Bash completion as given by kubectl completion -h:

  1. I installed bash-completion via Homebrew
  2. In my ~/.bashrc, I first source bash-completion then output from the completion kubectl subcommand:
    • source $(brew --prefix)/etc/bash_completion
    • source <(kubectl completion bash)

With these in place, I start up a new shell but the completion doesn't work. How do I get it working?

-- Dmitry Minkovsky
bash
bash-completion
homebrew
kubectl
kubernetes

4 Answers

7/21/2017

See the "On macOS, using bash" section of kubectl documentation: https://kubernetes.io/docs/tasks/tools/install-kubectl/#on-macos-using-bash I recently contributed those so they should be up to date. If not, please send a pull request to fix it.

Also: https://blog.fabric8.io/enable-bash-completion-for-kubernetes-with-kubectl-506bc89fe79e

-- AhmetB - Google
Source: StackOverflow

4/1/2020
  1. After brew install bash-completion, to actually enable bash completions, you need to:

    source /usr/local/etc/profile.d/bash_completion.sh

    Add that line to you bashrc.

  2. Then you can:

    source <(kubectl completion bash)
-- Anselme
Source: StackOverflow

7/21/2017

Once bash-completion is installed by Homebrew, it appears that its completions need to reside in $(brew --prefix)/etc/bash_completion.d. There you'll find a lot of other completions that come bundled. To add the completion for kubectl:

$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

That did the trick for me.

-- Dmitry Minkovsky
Source: StackOverflow

6/13/2019

I the answer form Ahmet B, the fix says to add the following to your .bashrc file:

export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

However, the install of completions 2:

brew install bash-completion@2

finishes with a message to add the export line if you would LIKE TO USE V1 completions. Removing that export enabled kubectl completion for me.

-- David J Arnone
Source: StackOverflow