'Why doesn't kubectl bash completion work on macOS/OS X?
I followed the instructions for installing Bash completion as given by kubectl completion -h:
- I installed
bash-completionvia Homebrew - In my
~/.bashrc, I first sourcebash-completionthen output from the completion kubectl subcommand:source $(brew --prefix)/etc/bash_completionsource <(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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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
Solution 4:[4]
- After
brew install bash-completion, to actually enable bash completions, you need to:
Add that line to yousource /usr/local/etc/profile.d/bash_completion.shbashrc. - Then you can:
source <(kubectl completion bash)
Solution 5:[5]
The above answers didn't work for me, but I found out this solution:
source /dev/stdin <<<"$(kubectl completion bash)"
Solution 6:[6]
Here is an alternative approach:
Install bash and bash-completion
brew install bash bash-completion
Set Terminal to use bash
Preferences > General > Shell open with: Command (complete path):
/opt/homebrew/bin/bash
Check the location bash-completion has been installed to
brew info bash-completion
Bash completion has been installed to: /opt/homebrew/etc/bash_completion.d
Edit ~/.bashrc
alias k="kubectl"
complete -F __start_kubectl k
source /opt/homebrew/etc/profile.d/bash_completion.sh
source <(kubectl completion bash)
Reload Terminal and verify that the completion works
~$ k <TAB>
alpha auth cordon diff get patch run version annotate autoscale cp ...
Environment
- macOS Monterey 12.2.1
- Homebrew 3.3.15
- GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
- bash-completion: stable 1.3 (bottled)
- kubectl v1.23.3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitry Minkovsky |
| Solution 2 | slfan |
| Solution 3 | ahmet alp balkan |
| Solution 4 | Philippe Fanaro |
| Solution 5 | Alex Weitz |
| Solution 6 |
