在 linux 中,通常我们可以使用<tab><tab>来进行补全,但是我们自己写的脚本却没有自动补全的功能。我们通过 linux 内置的 complete 命令来设定自定义补全。
一般情况下我们定义一个配置文件专门用于自定义配置文件
1 | #/usr/bin/env bash |
要使其生效,我们需要source config.file,我们可以将这个命令追加到.bash_profile中
用法
complete -W [wordlist] dothis例如: complete -W "now tomorrow never" dothis
$ dothis
<tab><tab>never now tomorrowcomplete -d dothis补全目录complete -e dothis补全文件complete -F function dothis根据函数 function 的中定义的值去显示,该值是实时生效的例如:
1
2
3
4
5#/usr/bin/env bash
list(){
COMPREPLY=(`cat .list`)
}
complete -F list dothis当我们修改
.list文件的内容时,补全也跟着变动