bash 4.4.20使用complete -I选项报错,是否4.0起支持该选项?
问题描述
我编写了如下bash补全函数__cmp_exec:
__cmp_exec() { local curr="$2" case "$curr" in ./*) local selected=$(find "." ! -type d -executable | fzf --query "$curr") if [[ -n "$selected" ]]; then COMPREPLY=( "$selected" ) else COMPREPLY=() fi esac } complete -I -F __cmp_exec -o bashdefault
本地运行一切正常,但在服务器上执行时出现错误:
bash: complete: -I: invalid option
服务器的bash版本为:
$ bash --version GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2016 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.
我原本认为bash从4.0版本开始支持-I选项,这个假设是否错误?
解答
你的假设没错,bash确实从4.0版本开始引入了complete命令的-I选项,报错的原因是选项顺序不符合旧版本bash的要求。
在bash 4.4及更早版本中,complete命令对选项顺序有严格限制:动作类选项(比如指定补全函数的-F)必须放在类型类选项(比如-I)的前面。你把-I放在-F之前,导致旧版本bash无法识别这个选项。
修正后的complete命令调整选项顺序即可:
complete -F __cmp_exec -I -o bashdefault
另外补充:bash 5.0及之后版本放宽了选项顺序的限制,所以你本地环境如果是bash 5.0+,原来的命令就能正常运行。
内容的提问来源于stack exchange,提问作者glades
相关产品推荐
相关产品推荐

