当参数中不包含特殊字符时,我已经使用 compgen 机制成功地为我的自定义命令实现了 bash 补全:
current=${COMP_WORDS[COMP_CWORD]}
all=$(_get_all_items)
COMPREPLY=( $(compgen -W "$all" -- $current) )
...
complete -F _prog_compl prog
我使用相同的方法来完成以冒号字符开头的项目::first
, :second
,...但它无法向我显示/自动完成它们。我尝试用反斜杠转义冒号,但这也不起作用。我应该如何在完成时转义冒号?
这些项目以冒号开头,比方说::first
, :second
。如果我写 progname 并以冒号开头,如下所示:
$ progname :<Tab here after colon>
I see 没有完成但两个冒号(“::”) - 其中一个自动添加到我键入的行中。如果我将冒号转换为普通字符(比方说“b”),我会得到完全符合我想要的结果:bfirst
, bsecond
...
值得注意的是,当我按 Tab 时,它会在我之前插入的冒号旁边放置另一个“:”,并且它会变成“::”。
$ bash --version
GNU bash, version 4.1.10(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
此外,我在 shell 中做了一些实验,结果如下:
$ compgen -W ":aaaa5 :gb2 :cd3" -- ":"
:aaaa5
:gb2
:cd3
然而奇怪的是,它在我唯一的“:”上添加了另一个“:”,并在我在命令行上输入 : 后将其变为“::”。
$ complete
complete -F _alp_compl alp.sh
complete -o nospace -F __dbus_send dbus-send
complete -F _myprog_compl myprog
complete -o nospace -F __gdbus gdbus
complete -o nospace -F __gsettings gsettings