6

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
  • 36,185
  • 26
  • 116
  • 160

7 Answers7

10

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
  • 36,185
  • 26
  • 116
  • 160
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.

slfan
  • 8,950
  • 115
  • 65
  • 78
1

The above answers didn't work for me, but I found out this solution:

source /dev/stdin <<<"$(kubectl completion bash)"
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • I tried this on bash 3.2 with macOS 11.6, and it didn't work - the 'source from stdin' part works with 3.2, but I believe the kubectl-generated bash script (from parenthesised part) [now requires](https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-mac/) bash 4.2+ and bash-completion v2. – RichVel Sep 06 '22 at 12:03
1

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
rbento
  • 9,919
  • 3
  • 61
  • 61
0

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

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
0
  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)
    
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Anselme
  • 101
  • 1
  • 2
0

managed to get it working in MacOS 12.5 by fixing some file permissions. using bash 5.1.16 in terminal type:

brew install bash
echo $BASH_VERSION
5.1.16(1)-release

chsh -s /opt/homebrew/Cellar/bash/5.1.16/bin/bash
brew install bash-completion@2
ls -la /opt/homebrew/etc/profile.d/bash_completion.sh

lrwxr-xr-x  1 myuser  admin  68 Aug  2 17:19 /opt/homebrew/etc/profile.d/bash_completion.sh -> ../../Cellar/bash-completion@2/2.11/etc/profile.d/bash_completion.sh

but the symbolic link target! had no executable flag so:

chmod +x /opt/homebrew/Cellar/bash-completion\@2/2.11/etc/profile.d/bash_completion.sh

then edit your ~/.bash_profile :

if [[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]]; then 
source /opt/homebrew/etc/profile.d/bash_completion.sh
echo "installed bash auto completions"
else
echo "Huston we have an auto compl problem"
fi

source <(kubectl completion bash)
alias k=kubectl
taitelman
  • 612
  • 5
  • 9