kubernetes: kubectl -bash: _init_completion: command not found

I’m having trouble with kubectl bash completion.

$ _output/local/bin/darwin/amd64/genbashcomp /usr/local/etc/bash_completion.d/

Error message when pressing tab:

$ kubectl -bash: _init_completion: command not found

I’m using bash_completion v1.3 from brew and kubectl v1.1.0-alpha.0.1417+c8177399965a6b

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 26 (18 by maintainers)

Most upvoted comments

On Mac, you have to install bash-completion with brew i.e. brew install bash-completion then as per the install notes, add it to your ~/.bash_profile:

  if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
  fi

That said even after doing that, it still doesn’t work for me but I guess it isn’t a kubectl issue.

After having done all things on https://kubernetes.io/docs/tasks/tools/install-kubectl/ on my iMac, I still got error: type _init_completion not found. And finally I found that, although I have update my bash, and I did got this message:

$which bash
/usr/local/bin/bash
$bash --version
GNU bash, version 5.0.7(1)-release (x86_64-apple-darwin18.5.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

BUT, my terminal was still using old bash in fact:

$env
SHELL=/bin/bash
$/bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

Need to change my default shell by typing command

chsh -l /usr/local/bin/bash

and it work.

Doesn’t work for me: CentOS 7.3, bash-completion 2.1-6.el7, kubectl-1.7.3-1

I’m sorry, I don’t have a mac to reproduce, so my ‘help’ might not be as helpful as it otherwise could be. But I can tell you how this works on Linux systems and hopefully that will get you an answer…

So _init_completion is supplied in bash_completions v1.3 as you pointed out. On Linux its in the file /usr/share/bash-completion/bash_completion. That file has to be sourced by your bash shell before you load the kubectl completions. Again on my linux system this works because ~/.bashrc sources /etc/bashrc. /etc/bashrc in turn walks /etc/profile.d/* and sources those files. In my case there is a /etc/profile.d/bash_completion.sh which checks for the existence of /usr/share/bash-completions/bash_completion and sources it if it exists.

I have no idea how that chain is supposed to work on your mac. But find the bash_completion file provided by the bash_completions v1.3 and source it, then see if the completions work for you.

it’s 2023, if you’re still getting this error on macos try another package called bash-completion@2

brew unlink bash-completion
brew install bash-completion@2
exit

Not working on Google Cloud Debian VM with bash-completion 2.1-4.3.

Re: support vs bugs, I did find some issues have been closed lately that are real issues, not requests for support. It doesn’t give the best image of the project (which is a shame as kubernetes is awesome) but I can understand as it can be hard to manage the number of issues and number of support type issues that are a better fit for SO. Thanks for your help with that, I’ll answer the question on SO.