Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38143

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:

  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?

Upvotes: 6

Views: 5069

Answers (8)

stzov
stzov

Reputation: 176

One reason auto-completion does not work could be that you use Bash 3.2 which is not compatible with any of the bash-completion versions.

The only way fix your auto-completion issue if you use Bash 3.2 is to upgrade your Bash version.

Warning: There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The kubectl completion script doesn't work correctly with bash-completion v1 and Bash 3.2. It requires bash-completion v2 and Bash 4.1+. Thus, to be able to correctly use kubectl completion on macOS, you have to install and use Bash 4.1+ (instructions). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer). Source: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/

Fixing the issue with

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

will work only if you've installed bash-completion via other means.

Upvotes: 0

taitelman
taitelman

Reputation: 700

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

Upvotes: 0

rbento
rbento

Reputation: 11608

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

Upvotes: 1

AlexW
AlexW

Reputation: 3399

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

source /dev/stdin <<<"$(kubectl completion bash)"

Upvotes: 1

Anselme
Anselme

Reputation: 141

  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)
    

Upvotes: 0

David J Arnone
David J Arnone

Reputation: 21

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.

Upvotes: 2

ahmet alp balkan
ahmet alp balkan

Reputation: 45224

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

Upvotes: 0

Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38143

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.

Upvotes: 10

Related Questions