我按照
kubectl completion -h
: 给出的安装 Bash 补全的说明进行操作
bash-completion
~/.bashrc
中,我首先获取
bash-completion
,然后从完成 kubectl 子命令输出:
source $(brew --prefix)/etc/bash_completion
source <(kubectl completion bash)
bash-completion
由 Homebrew 安装,看起来它的完成 需要居住在
$(brew --prefix)/etc/bash_completion.d
。在那里您会发现许多其他捆绑的完成品。添加 kubectl 的补全:
$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
这对我有用。
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"
但是完成2的安装:
brew install bash-completion@2
如果您想使用 V1 补全,则会显示一条消息以添加导出行。删除该导出为我启用了 kubectl 完成功能。
安装 bash 和 bash-completion
brew install bash bash-completion
设置终端使用 bash
Preferences
>
General
>
Shell open with: Command (complete path)
:
/opt/homebrew/bin/bash
检查 bash-completion 已安装到的位置
brew info bash-completion
Bash补全已安装到: /opt/homebrew/etc/bash_completion.d
编辑~/.bashrc
alias k="kubectl"
complete -F __start_kubectl k
source /opt/homebrew/etc/profile.d/bash_completion.sh
source <(kubectl completion bash)
重新加载终端并验证是否完成工作
~$ k <TAB>
alpha auth cordon diff 获取补丁运行版本 注释自动缩放 cp ...
环境
https://kubernetes.io/docs/tasks/tools/install-kubectl/#on-macos-using-bash 我最近贡献了这些,所以它们应该是最新的。如果没有,请发送拉取请求来修复它。
另外:https://blog.fabric8.io/enable-bash-completion-for-kubernetes-with-kubectl-506bc89fe79e
brew install bash-completion
之后,要真正启用 bash 补全,您需要:
source /usr/local/etc/profile.d/bash_completion.sh
添加该行给您
bashrc
。
source <(kubectl completion bash)
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
但是符号链接目标!没有可执行标志,所以:
chmod +x /opt/homebrew/Cellar/bash-completion\@2/2.11/etc/profile.d/bash_completion.sh
然后编辑您的 ~/.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
如果您使用 Bash 3.2,解决自动完成问题的唯一方法是升级您的 Bash 版本。
警告:bash-completion 有两个版本:v1 和 v2。 V1 适用于 Bash 3.2(这是 macOS 上的默认版本),v2 适用于 Bash 4.1+。 kubectl 完成脚本无法与 bash-completion v1 和 Bash 3.2 一起正常工作。它需要 bash-completion v2 和 Bash 4.1+。因此,为了能够在 macOS 上正确使用 kubectl 补全,您必须安装并使用 Bash 4.1+(说明)。以下说明假设您使用 Bash 4.1+(即 4.1 或更高版本的任何 Bash 版本)。 来源:解决问题https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/
$ kubectl 完成 bash > $(brew --prefix)/etc/bash_completion.d/kubectl仅当您通过其他方式安装了 bash-completion 时才有效。