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

Viewed 3934

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?

7 Answers

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.

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

source /dev/stdin <<<"$(kubectl completion bash)"
  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)
    

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

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
Related