#Bash 的 complete 命令
complete [OPTION]... [NAME]...
功能
指定按 TAB 时 Readline 如何补全参数。
类型
Bash 内置命令。
参数
OPTION- 选项-a- 用 别名 补全参数-b- 用 内置命令 补全参数-c- 用 命令 补全参数-d- 用 目录 补全参数-e- 用 环境变量名 补全参数-f- 用 文件 补全参数-g- 用 组名 补全参数-j- 用 作业名 补全参数-k- 用 关键字 补全参数-s- 用 服务名 补全参数;参考/etc/services-u- 用 用户名 补全参数;参考/etc/passwd-v- 用 变量名 补全参数-o option- 开启选项nospace- 补全后不自动添加空格filenames- 告知 Bash 结果是文件名;补全过程中会在目录后加上/bashdefault- 如果没找到匹配项,则回退到 Bash 默认补全default- 如果没找到匹配项,则回退到默认行为dirnames- 如果没有匹配项,则尝试进行目录名补全
+o option- 关闭选项-A action- 补全方式alias- 用 别名 补全参数;同-a选项builtin- 用 内置命令 补全参数;同-b选项command- 用 命令 补全参数;同-c选项directory- 用 目录 补全参数;同-d选项export- 用 环境变量名 补全参数;同-e选项file- 用 文件 补全参数;同-f选项group- 用 组名 补全参数;同-g选项job- 用 作业吗 补全参数;同-j选项keyword- 用 关键字 补全参数;同-k选项service- 用 服务 补全参数;同-s选项
-G globpat- 补全通配符字符串globpat-W wordlist- 以wordlist作为补全列表-F function- 通过函数function生成补全列表-C command- 通过命令command生成补全列表-X filterpat- 根据filterpat从补全列表中滤除部分选项-P prefix- 为补全的结果添加前缀-S suffix- 为补全的结果添加后缀-p- 打印补全规则-r- 删除补全规则-D- 设置默认的补全方式;通常是文件-E- 设置命令行为空时的补全方式;通常是命令-I- 设置开始词的补全方式;通常是命令
NAME- 要配置的命令列表
#示例
用命令补全参数
$ mycmd # 按 TAB 补全,默认补全为当前路径下的文件
1.txt 2.txt 3.txt dir1 dir2
$ complete -c mycmd # 指定用 命令 补全 mycmd 的参数
$ mycmd # 按 TAB 补全,补全为命令
Display all 3318 possibilities? (y or n)
addpart b2sum cat date echo
agetty base32 chcon dd env
alias base64 chgrp df expand
...
用目录补全参数
$ mycmd # 按 TAB 补全,默认补全为当前路径下的文件
1.txt 2.txt 3.txt dir1 dir2
$ complete -d mycmd # 指定用 目录 补全 mycmd 的参数
$ mycmd # 按 TAB 补全,补全为命令
dir1 dir2
指定参数补全列表
$ mycmd # 按 TAB 补全,默认补全为当前路径下的文件
1.txt 2.txt 3.txt dir1 dir2
$ complete -W "start stop restart" mycmd # 指定补全列表为 start stop restart
$ mycmd # 按 TAB 补全
restart start stop
$ mycmd st # 按 TAB 补全
start stop
通过通配符补全
$ mycmd # 按 TAB 补全,默认补全为当前路径下的文件
1.txt 2.txt 3.txt 1.md 2.md
$ complete -G '*.md' mycmd # 指定用 *.md 补全 mycmd 的参数
$ mycmd # 按 TAB 补全
1.md 2.md
指定前后缀
$ mycmd # 按 TAB 补全,默认补全为当前路径下的文件
1.txt 2.txt 3.txt 1.md 2.md
$ complete -G '*.md' -P '-i ' -S ' -o' mycmd # 指定用 -i *.md -o 补全 mycmd 的参数
$ mycmd # 按 TAB 补全
-i 1.md -o -i 2.md -o
#相关命令
#手册
complete: complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
Specify how arguments are to be completed by Readline.
For each NAME, specify how arguments are to be completed. If no options
are supplied, existing completion specifications are printed in a way that
allows them to be reused as input.
Options:
-p print existing completion specifications in a reusable format
-r remove a completion specification for each NAME, or, if no
NAMEs are supplied, all completion specifications
-D apply the completions and actions as the default for commands
without any specific completion defined
-E apply the completions and actions to "empty" commands --
completion attempted on a blank line
-I apply the completions and actions to the initial (usually the
command) word
When completion is attempted, the actions are applied in the order the
uppercase-letter options are listed above. If multiple options are supplied,
the -D option takes precedence over -E, and both take precedence over -I.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.