Reputation: 38143
I followed the instructions for installing Bash completion as given by kubectl completion -h
:
bash-completion
via Homebrew~/.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
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
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
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
Upvotes: 1
Reputation: 3399
The above answers didn't work for me, but I found out this solution:
source /dev/stdin <<<"$(kubectl completion bash)"
Upvotes: 1
Reputation: 141
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
.source <(kubectl completion bash)
Upvotes: 0
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
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
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