Bash - Complete (Builtin command) - Completion

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU) > Bash - Text Editing

1 - About

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence

The process of applying these completion specifications when word completion is attempted is described above under Programmable Completion.

Advertising

3 - Syntax

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 ...]

Specify how arguments to each name should be completed.

  • p (or if no options are supplied) existing completion specifications are printed in a way that allows them to be reused as input.
  • r removes a completion specification for each name, or, if no names are supplied, all completion specifications.

Other options, if specified, have the following meanings.

The arguments to the -G, -W, and -X options (and, if necessary, the -P and -S options) should be quoted to protect them from expansion before the complete builtin is invoked.

3.1 - -o comp-option

The comp-option controls several aspects of the compspec’s behavior beyond the simple generation of completions. comp-option may be one of:

  • bashdefault: Perform the rest of the default bash completions if the compspec generates no matches.
  • default. Use readline’s default filename completion if the compspec generates no matches.
  • dirnames: Perform directory name completion if the compspec generates no matches.
  • filenames Tell readline that the compspec generates file names, so it can perform any filename-specific processing (like adding a slash to directory names or suppressing trailing spaces). Intended to be used with shell functions.
  • nospace Tell readline not to append a space (the default) to words completed at the end of the line.
  • plusdirs After any matches defined by the compspec are generated, directory name completion is attempted and any matches are added to the results of the other actions.
Advertising

3.2 - -A action

The action may be one of the following to generate a list of possible completions:

  • alias Alias names. May also be specified as -a.
  • arrayvar Array variable names.
  • binding Readline key binding names.
  • builtin Names of shell builtin commands. May also be specified as -b.
  • command command| names. May also be specified as -c.
  • directory Directory names. May also be specified as -d.
  • disabled Names of disabled shell builtins.
  • enabled Names of enabled shell builtins.
  • export: Names of exported shell variables. May also be specified as -e.
  • file: File names. May also be specified as -f.
  • function: Names of shell functions.
  • group Group names. May also be specified as -g.
  • helptopic Help topics as accepted by the help builtin.
  • hostname Hostnames, as taken from the file specified by the HOSTFILE shell variable.
  • job Job names, if job control is active. May also be specified as -j.
  • keyword Shell reserved words. May also be specified as -k.
  • running Names of running jobs, if job control is active.
  • service Service names. May also be specified as -s.
  • setopt Valid arguments for the -o option to the set builtin.
  • shopt Shell option names as accepted by the shopt builtin.
  • signal Signal names.
  • stopped Names of stopped jobs, if job control is active.
  • user User names. May also be specified as -u.
  • variable Names of all shell variables. May also be specified as -v.
Advertising

3.3 - -G globpat

The filename expansion pattern globpat is expanded to generate the possible completions.

3.4 - -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.

3.5 - -C command

command is executed in a subshell environment, and its output is used as the possible completions.

3.6 - -F function

The shell function function is executed in the current shell environment. When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable.

3.7 - -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.

3.8 - -P prefix

prefix is added at the beginning of each possible completion after all other options have been applied.

3.9 - -S suffix

suffix is appended to each possible completion after all other options have been applied.

4 - Return value

The return value is true unless an invalid option is supplied, an option other than -p or -r is supplied without a name argument, an attempt is made to remove a completion specification for a name for which no specification exists, or an error occurs adding a completion specification.

5 - Example

5.1 - Auto-completion of script option