Search code examples
linuxcommand-line-interfacebash-completionsles

How to autocomplete the options for a command in SLES 12?


I have a service, say xyz. As of now, I can start, stop or restart that service using the command rcxyz start, rcxyz stop and rcxyz restart respectively. It is working fine.

All that I want to do is, if I type rcxyz and press <TAB>, I would like to have the three options (start, stop and restart) to be auto completed.

I referred the other bash_completion related questions in stackoverflow and tried writing a dummy script /etc/bash_completion.d/rcxyz.sh inside which I have written

complete -c rcxyz

I was expecting that when I type rcxyz and press <TAB>, I will get the list of available commands as argument. But it didn't work.

Am I missing something ?


Solution

  • You should write:

    complete -W 'start stop restart' rcxyz
    

    See bash's manual:

    -W wordlist

    The wordlist is split using the characters in the IFS special variable as delimiters, and each resultant word is expanded. The possible completions are the members of the resultant list which match the word being completed.