2

Can't seem to find a man complete page, so was hoping folks could help explain the options in the complete command (from complete --help):

complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
Specify how arguments are to be completed by Readline.

For each NAME, specify how arguments are to be completed.  If no options
are supplied, existing completion specifications are printed in a way that
allows them to be reused as input.

Options:
  -p        print existing completion specifications in a reusable format
  -r        remove a completion specification for each NAME, or, if no
            NAMEs are supplied, all completion specifications
  -D        apply the completions and actions as the default for commands
            without any specific completion defined
  -E        apply the completions and actions to "empty" commands --
            completion attempted on a blank line

When completion is attempted, the actions are applied in the order the
uppercase-letter options are listed above.  The -D option takes
precedence over -E.

I am trying to have BOTH the commands for a bash script as option 1 to complete and (ideally when appropriate) valid files and/or directories next. Is this possible using complete or do I need a different approach?

Right now, I have this:

complete -W "mount unmount" phone

...for a script called phone.

1 Answer 1

1

On your request "help explain the options in the complete command":

It is a longer story to tell all details, hence find a list of hopefully useful documentation references at the end of this post.

About your specific use case to complete $1 as command and $2 as files, I have added a small example in the next section.

Example

Write a completion function that handles positional differences:

# completion function to differentiate type of completion by argument type
_complete_phone () {
  case ${#COMP_WORDS[@]} in
    1) # 1st position is the `phone` command itself
       return;;
    2) # 2nd position are sub-commands, so we use `-W` for word completion
       words="mount unmount"
       COMPREPLY=($(compgen -W "$words" -- "${COMP_WORDS[COMP_CWORD]}"))
       ;;
    *) # other arguments filter files, either
       # any files using option `-f`
       #COMPREPLY=($(compgen -f -- "${COMP_WORDS[COMP_CWORD]}"))
       # and for specific files (i.e. pattern `*.phone`), use words again
       COMPREPLY=($(compgen -W "$(ls *.phone 2>/dev/null)" -- "${COMP_WORDS[COMP_CWORD]}"))
       ;;
  esac
}

Then activate completion by function activated with the -F argument, using your completion function name:

# complete using your function 
complete -F _complete_phone phone

And finally, try (for reproducibility an arbitrary program phone and 'relevant' files have to be created):

# try
phone () { echo "hello, phone program will $@"; }
touch {my,your}.phone
phone m\tab \tab \tab

Documentation

Bash Reference Manual

An excellent go-to resource of documentation when you want more details about bash.

Specific for bash completion, have a look into chapters

Source: www.gnu.org

Bash Manual

Run man bash and go to sections:

  • READLINE

    • Readline Variables explaining how to set variables related to completion. For example set completion-ignore-case on to "perform filename matching and completion in case insensitive fashion".
    • Completing default bash completion.
    • Programmable Completion explaining the process of completion, and relevant elements such as methods compgen, variables COMP_WORDS, etc. you find in the example above.
  • SHELL BUILTIN COMMANDS

    • compgen to generate completion matches.
    • complete to specify how to complete. Also describes all options, such as -W, -F, -o, etc.
    • compopt to set other completion options.

Source: GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.