61

From help compgen:

$ help compgen
compgen: compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
    Display possible completions depending on the options.
    
    Intended to be used from within a shell function generating possible
    completions.  If the optional WORD argument is supplied, matches against
    WORD are generated.
    
    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

What do options [-abcdefgjksuv] stand for?

In other words, I want to know how to use all options.

3
  • Have you read the man page? man compgen. The man page is for the shell builtins, so you'll have to scroll down to compgen. Aug 20, 2014 at 10:35
  • 14
    "man compgen", does not exist. use "man bash"
    – Kevin
    Sep 11, 2015 at 20:16
  • You can try help compgen.
    – Darkov
    Mar 24, 2020 at 14:00

2 Answers 2

63

Options for compgen command are the same as complete, except -p and -r. From compgen man page:

compgen
 compgen [option] [word]
 Generate possible completion matches for word according to the options, which 
 may be any option accepted by the complete builtin with the exception of -p 
 and -r, and write the matches to the standard output

For options [abcdefgjksuv]:

  • -a means names of aliases
  • -b means names of shell builtins
  • -c means names of all commands
  • -d means names of directories
  • -e means names of exported shell variables
  • -f means names of files
  • -g means names of groups
  • -j means names of jobs
  • -k means names of shell reserved words
  • -s means names of services
  • -u means names of user names
  • -v means names of shell variables

You can see the complete man page here.

6
  • @Pandya: See options for complete, it's the same as compgen, except -p and -r.
    – cuonglm
    Aug 20, 2014 at 10:50
  • I've read it from man bash and suggested edit to complete your post so-that I easily accept.
    – Pandya
    Aug 20, 2014 at 11:02
  • 1
    Any information on how to use compgen -G? Say my pattern is compgen -G '*.zip' and I have the file "foo.zip". compgen will find foo.zip, but what if the user has already typed 'b'. Now, compgen should find nothing. If I give the command compgen -G '*.zip' "b", compgen still finds foo.zip Jun 22, 2016 at 20:04
  • @cuonglm How to list functions that are provided by the service command? For example, using service networking and hitting tab will return force-reload reload restart start stop. Do you know how to get those using compgen?
    – Ilia
    Feb 11, 2017 at 8:43
  • @IliaRostovtsev: see this one for example github.com/scop/bash-completion/blob/…
    – cuonglm
    Feb 11, 2017 at 9:05
9

At O'Reilly there is a book (bash Quick Reference by Arnold Robbins) that also has the hint form compgen options being described at complete. Going for the complete documentation gives a few other options than listed above. They might not be related to a current versions, BUT there are options no longer or not described in the current version docs.

As of now I have seen on the web these upper case options being used for invocation of compgen or mentioned in complete docs!

compgen [option] [word]
complete [-abcdefgjksuv] [-o comp-option] [-A action] [-G globpat] [-W wordlist]
      [-P prefix] [-S suffix] [-X filterpat] [-F function]
      [-C command] name [name ...]
      complete -pr [name ...]
  • -A action = The action may be one of the following to generate a list of possible completions: (see links)
  • -C command = Run command in a subshell and use its output as the list of completions.
  • -F function = Run shell function in the current shell. Upon its return, retrieve the list of completions from the COMPREPLY array.
  • -G globpat = The filename expansion pattern globpat is expanded to generate the possible completions.
  • -P prefix = prefix is added at the beginning of each possible completion after all other options have been applied.
  • -S suffix = suffix is appended to each possible completion after all other options have been applied.
  • -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.
  • -X filterpat = filterpat is a pattern as used for filename expansion. It is applied to the list of possible completions generated by the preceding options and arguments, and each completion matching filterpat is removed from the list. A leading ‘!’ in filterpat negates the pattern; in this case, any completion not matching filterpat is removed.

A rather exhaustive looking version of the documentation can be found in Programmable Completion Builtins.

As both commands are built in command, the official documentation is on your local man page for bash. Use the search option for locating the relevant parts in it. Sorry, but the layout looks somewhat broken (usability reduced in the generated layout) in either view variant, even on the internet services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.