有许多可用资源(1、2、3)解释了如何利用 bash 自动完成命令和参数的能力,但所有这些资源都需要向用户添加代码,~/.bash_profile
或者/etc/bash_completion.d/*
有没有办法使一个脚本及其可用的完成是独立的?作为一个粗略和不完整的例子:
~/bin/script-with-integrated-autocomplete.sh:
#!/usr/bin/env bash
function _completions {
complete ...
}
if [ "$1" == "completions" ]; then
_completions
exit 0
fi
# (Whatever the script really does goes here.)
# ...
# ...
Run Code Online (Sandbox Code Playgroud)
交易破坏者(在这个问题的上下文中)是上面的例子仍然需要你添加类似的东西~/bin/script-with-integrated-autocomplete.sh completions
来.profile
完成。
有没有办法让单个 bash 脚本(文件)声明自己的完成并让 bash在调用时识别它们(理想情况下没有额外的系统或环境设置)?