Issue
I'm running on Ubuntu 18.04 and use zsh instead of bash. To get kubectl completion it should be as simple as adding
source <(kubectl completion zsh)
to the ~/.zshrc config. I have done so, but the kubectl auto-completion is not working. I have reloaded the terminal several times.
When changing the command to
source <(kubectl completion zsh) && echo success || echo failure
I see "failure" on loading the terminal. So my conclusion would be that something goes wrong with the script. The script dismisses all the output, so I don't know what's going wrong exactly.
Does anyone know what might be wrong? Or maybe where the specific script is located, so I can change
_complete kubectl 2>/dev/null
to
_complete kubectl 2>~/logs.txt
Solution
Keep in mind that the following command:
source <(kubectl completion zsh)
simply sources the output of the command:
kubectl completion zsh
which generates kubectl autocompletion script
and prints it to the standard output. Instead of using it that way you may simply redirect its output to the file:
kubectl completion zsh > kubectl-autocompletion-script
change the line:
_complete kubectl 2>/dev/null
to:
_complete kubectl 2>~/logs.txt
or any other part of the script you like.
It's difficult to guess what might be wrong in your particular case but you can easily debug the script. Just for debugging purposes you may try to run it as any other shell script. If there are any errors when sourcing the script (which is basically running it in the current shell) you'll see those errors also when you run it in a new shell which of course will not affect the environment of your currently running shell but will tell you what might be the issue.
To turn on debugging mode you need to use -x
flag (or -xv
for verbose output):
zsh -x kubectl-autocompletion-script
This should tell you where the problem is located.
One more thing: As alternative to adding source <(kubectl completion zsh)
to your ~/.zshrc
you may want to place the script in /etc/zsh/zshrc.d/
which basically do the same but globally, for all users.
Answered By - mario Answer Checked By - David Marino (WPSolving Volunteer)