How do I debug bash completions?

set -x

If you set -x either in the interactive session or the autocomplete script itself, (nearly?) every command and its results will be printed out. This includes the work done inside the autocomplete script.

This can then be quieted back down again with set +x.

-x

After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

-from Bash manual #The Set Builtin


I used this simple function to redirect everything I need to file.

function debug_me {
    echo $@
    echo "COMP_CWORD:" $COMP_CWORD
    echo "COMP_POINT:" $COMP_POINT
    echo "COMP_LINE:" $COMP_LINE
    echo "COMP_LINE#:" ${#COMP_LINE}
    echo "COMP_KEY:" $COMP_KEY
    echo "COMP_WORDS:" ${COMP_WORDS[@]}
    echo "COMPREPLY:" ${COMPREPLY[@]}
    echo
} >> ~/com.debug

And call it as: debug_me $someadditional_variable "If should be true here"


Just log / output from the bash completion functions; as they are most likely no rocket science, this should be fairly easy to do. It might be possible to connect bashdb, but it feels unnecessary...