In the bash completion setup (https://github.com/scop/bash-completion) supplied by my system there seems to be an unequivocal preferences towards completing with complete -F
as opposed to complete -C
.
complete -p | grep -- -C # no match
The difference between the two appears to be that
the -F
unction-taking form expects a function that reads input from variables and outputs to variables wheres the -C
ommand-taking form reads input from positional arguments and outputs to stdout
.
I like the second approach better as it's more functional and the completions functions/programs are slightly easier to debug (as they don't depend on a particular context of variables).
However, https://github.com/scop/bash-completion's preference towards the former makes me wonder: are there any downsides to the -C
approach apart from the obvious (negligible for a UI, IMHO) performance overhead of a new process?
Best Answer
I'm not sure why you'd consider
complete -C {command}
"more functional" thancomplete -F {function}
, since the latter literally takes a function; so the motivation is not clear for your stated preference. But you're right, almost all examples and documentation show a preference forcomplete ... -F {function} ...
, e.g.,complete -F _foobar foobar
, where the function_foobar
is used to generate completion options for a hypothetical commandfoobar
.Main reasons are, presumably,
_foobar
" can be used to complete multiple commands, e.g., perhaps afoobar
package also has afoo
and abar
command;$1
is passed to the function so you know the name of the command being completed.-C
" variant seems dumber by comparison (but, admittedly, I've always used the "-F
" variant myself.)complete
statement, or it'd call an external shell script, in which case you'd have to ship and keep track of the location of that script (a pain).Also, in my personal "completions", I have to get them working across older versions of bash and for various Unix's (AIX, HP-UX, Solaris, Linux, BSD), and I've found a lot of variety in what is supported. Since "
complete
" returnstrue
when it works, I use the following idiom which seems to be more portable across bash versions (I only included this here, since it shows the usage of "-F {function} {commmand}
" is simpler/cleaner than a corresponding "-C "command"
" would be):Likewise, if I have
cmd1
,cmd2
,cmd3
that all take basically the same completions (e.g., consider a completion script forscp
andssh
that take a list of hostnames which you generate),For reference: