Bash completion extra space when adding file directory

autocompletebashfedorascriptingshell-script

I have a script for extending bash-completion for certain commands. If I type command1 and do a tab, it should auto-complete with a file directory (and the files). It works fine expect for one inconvenient issue. It adds an extra white space every time it auto-complete. So if you are trying to reach sub-directories from root, for example, every sub-directory you get to an extra space gets added and it kinda defeats the purpose of extending bash-completion. I am new to bash. Is this a result of how I wrote the script or is this just how tab-completion behaves when you extend it?

local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="command1 command2"
serviceOpts="opt1 opt2"


case "${prev}" in
    command1)
        COMPREPLY=( $(compgen -f ${cur}) )
        return 0
        ;;
    command2)
        COMPREPLY=( $(compgen -W "${serviceOpts}" ${cur}) )
        return 0
        ;;
    *)
    ;;
esac

Best Answer

There is an option -o nospace to the complete command that tells Readline to not add that extra space. Docs

Related Question