I am experimenting with bash completion to write a general completion library in C associated with command line parsing and stumbled in a behavior for which I can find no documentation.
I am using the following scrip that I call compscript to make tests:
#!/bin/bash
>&2 echo
>&2 echo $3
>&2 echo $2
echo testing
This script is used as an external completion command with complete -C
and all it does is print to stderr its third argument ($3
) which is the string before the string being completed and its second argument ($2
) which is the string being completed, and then complete anything to the word "testing" (complete -C
takes completion strings from the stdout of the given command).
I then associate compscript as a completion command for a dummy, non-existent program with
complete -C ./compscript dummy
Now, if I type
dummy abc tes<tab>
compscript will print "abc" and "tes" to stderr and complete "tes" as testing, as expected (to see the completion, it is better to refresh the command line with Control-L).
However, if I type
dummy abc "tes<tab>
then compscript will print abc, as expected, and tes without the opening quotation mark, complete it as testing and bash (not compscript) will automatically close the quotation mark, effectively completing "tes as "testing".
My question is where do I find the documentation and specification for this and possibly many other corner case behaviors of bash completion? This subject seems very badly documented.
EDIT
I found yet another bash completion idiosyncrasy. If the following completion is being performed:
dummy abc def=ghi<tab>
Then, if '=' is not in COMP_WORDBREAKS, then $2='def=ghi' and $3='abc', and if '=' is in COMP_WORDBREAKS, then $2='ghi' $3='='.
However, it makes no difference whether '"' is or is not in COMP_WORDBREAKS. In both cases:
dummy abc def"ghi<tab> -> $2='ghi' and $3='abc'
dummy abc def="ghi<tab> -> $2='ghi' and $3='abc' ('=' not in COMP_WORDBREAKS)
dummy abc def="ghi<tab> -> $2='ghi' and $3='=' ('=' in COMP_WORDBREAKS)
Also, in case of closed quotes, it makes no difference whether '"' is or is not in COMP_WORDBREAKS:
dummy abc def"ghi"<tab> -> $2='def"ghi"' and $3='abc'
dummy abc def="ghi"<tab> -> $2='def"ghi"' and $3='abc' ('=' not in COMP_WORDBREAKS)
dummy abc def="ghi"<tab> -> $2='"ghi"' and $3='=' ('=' in COMP_WORDBREAKS)
Everything is the same for single quotes instead of double quotes.
It is puzzling that both single and double quotes are usually present in COMP_WORDBREAKS, as it makes no difference if they are there or not. It is also hard to understand why COMP_WORDBREAKS even exists, as modifying it can break some or even all completion scripts. It would be much more sensible if word break characters were simply the bash metacharacters.