complete Command in Linux



Linux, the powerful operating system that forms the backbone of countless servers and systems worldwide, offers a vast array of commands to manage and interact with the system. Among these is the complete command, an integral part of the Bash shell, which provides an auto-completion feature that can significantly enhance productivity and reduce the potential for errors when typing commands.

The complete command is a built-in Bash function that defines how arguments are completed by the Readline library. In simpler terms, it's responsible for the tab completion feature you see when you press the Tab key while typing commands in your terminal.

Table of Contents

Here is a comprehensive guide to understand the various options available with the complete command −

Understanding complete Command

The complete command is used to specify how arguments to a command should be completed. It can be used to define custom completions for commands where the default Bash completion may not suffice. in Linux, the complete command is a powerful tool used to set up auto-completion for other commands.

It allows users to automate the completion of command names, options, and arguments, enhancing the efficiency of command-line work. By typing a partial command and pressing the Tab key, the complete command helps to quickly fill in the rest based on the available options or paths.

Install complete Command

The complete command is a built-in function of Bash, not an external program. This means it's already part of your system if you're using Bash as your shell.

Ubuntu and Debian-based systems

sudo apt install bash-completion

CentOS, RHEL, and Fedora

sudo yum install bash-completion

Once installed, you might need to enable Bash completion by sourcing the appropriate file. This is typically done by adding the following line to your .bashrc file −

source /etc/bash_completion.d/bash_completion

To apply the changes immediately without logging out, you can run −

source ~/.bashrc

In this way, a complete command is installed in Linux.

How to Use complete Command in Linux?

The complete command in Linux is a powerful tool used in the Bash shell to control the behavior of the autocomplete feature. Autocomplete is a well-known feature that helps users complete commands, filenames, and other arguments quickly by pressing the Tab key.

Basic Usage

The basic syntax of the complete command is as follows −

complete [options] [name...]

Where name is the name of the command for which you want to define completion behavior. The complete command comes with a variety of options that determine how autocompletion should work.

Here's a detailed explanation of the options available for the complete command −

Options Descriptions
-A action Specify an action to generate possible completions. Actions can be things like file, directory, user, variable, signal, etc.
-G globpat Use a glob pattern to generate possible completions.
-W wordlist Use a list of words for possible completions.
-F function Use a shell function to generate possible completions.
-C command Use a command to generate possible completions.
-o option Include shell options like bash default, default, dirnames, filenames, nospace, plusdirs, etc.
-p Print current completion definitions in a format that can be reused as input.
-r Remove a completion definition for a command.
-D Apply completion behavior as the default for commands without any specific completion defined.
-E Apply completion behavior to "empty" commands (when no command has been typed yet).

The complete command functionality is particularly useful for long or complex commands, saving time and reducing the likelihood of errors during input.

Examples of complete Command in Linux

Here are some examples of how to use the complete command −

  • Filename Completion
  • Username Completion
  • Restricts the Completion to Filenames
  • Host Name Completion
  • Variable Name Completion
  • Custom Completion Definition
  • Completion with Alias
  • Job Completion
  • Service Completion
  • Dynamic Completion

Filename Completion

The complete -d command is used in Unix-like operating systems to specify directory name completion for a command, in this case, ls. The ls command itself is a fundamental utility that lists the contents of directories. When combined with the -d option, it would only list directories themselves, not their contents.

complete -d ls
Filename Completion complete Command

Username Completion

The command complete -u is used in Unix-like operating systems to manage auto-completion for commands, files, and variables in the shell. When combined with the write command, it could be configuring auto-completion for writing permissions or targeting a specific user with the -u flag −

complete -u write
Username Completion complete Command

Restricts the Completion to Filenames

The -f option restricts the completion to file names only. The ls command lists directory contents and the -l option provides a long listing format. This combination can be used to create an autocomplete script that suggests file names when typing ls -l into the terminal.

complete -f ls -l
Restricts Completion to Filenames

Host Name Completion

The command complete -A hostname ls is used in the context of the Bash shell to generate autocompletion suggestions for hostnames when typing the ls command.

complete -A hostname ls
Host Name Completion

Note − This allows for hostname completion when typing arguments for ls.

Variable Name Completion

The -v option for complete will list all completions that are defined for the shell environment. When combined with the ls command, it would typically show any completions that are specifically defined for ls, such as aliases or functions that enhance or modify the behavior of the ls command −

complete -v ls
Variable Name Completion

This sets up variable name completion for ls command.

Custom Completion Definition

The complete command ls is used to specify a custom autocompletion for a command in the shell, in this case, ls. The -C option tells the shell to run the specified command, rename_file.sh, to generate the list of possible completions −

complete -C rename_file.sh ls
Custom Completion Definition

Note − If you have a custom function that knows how to complete your command, you can specify it with -F −

complete -F _my_completion_function mycommand

Completion with Alias

The command alias my_ls='ls -la' creates a shortcut for the ls -la command, which lists files in the current directory in long format, including hidden files. The complete -F adds auto-completion for this alias by using the _longopt function

alias my_ls='ls -la'
complete -F _longopt my_ls
Completion with Alias

This applies the _longopt function to provide completions for the my_ls alias.

Job Completion

The complete command does not appear to be a standard command. However, if you're looking to list files and directories with detailed information including job control status, you might want to use the ls -l command. This command displays the files and directories in the long listing format, showing permissions, ownership, size, and modification date for each file −

complete -j ls -l
Job Completion complete Command

This enables job completion for ls -l command, useful for commands that manipulate jobs.

Service Completion

Here's an example of a complete command in Linux that follows the syntax you've provided: This command would enable shell command-line completion for the ssh command by suggesting service names. The -A service option tells the complete builtin to restrict the completions to service names known to the system −

complete -A service ssh
Service Completion complete Command

Note − This can be particularly useful for system administrators who need to remember service names quickly and accurately.

Dynamic Completion

This defines a function _my_completions that dynamically generates completions for command_name based on the options provided.

_my_completions() {
   COMPREPLY=($(compgen -W "option1 option2 option3" -- "${COMP_WORDS[COMP_CWORD]}"))
   }
complete -F _my_completions command_name

Help Page of complete Command

For further details on the complete command and programmable completion in Bash, you can refer to the Bash Reference Manual or the complete help page. Additionally, online resources like tutorials and community forums can provide practical examples and guidance −

complete --help
Help Page of complete Command

Advanced Usage of complete Command

For more advanced users, the complete command can be combined with Bash's programmable completion features to create sophisticated autocompletion behaviors. This can involve writing custom functions that dynamically generate completion options based on the current context, such as the already typed command-line arguments or the output of other commands.

The complete command offers many other options for fine-tuning completion behavior. You can control case sensitivity, filtering, and more.

Alternatives of complete Command

However, there are modern alternatives that offer enhanced features and improved performance. Here are three notable alternatives to the 'complete' command in Linux −

Zsh Autocompletion

Zsh, or the Z Shell, is a powerful shell that comes with an impressive autocompletion feature. It's not just an alternative to the complete command but a complete overhaul of the shell experience. Zsh's completion system is highly customizable and can be fine-tuned to a user's preference. It also includes features like spelling correction and context-aware suggestions.

Fish Shell

The Friendly Interactive Shell, or Fish, is another shell that provides intelligent autocompletion. It's designed to be user-friendly and offers suggestions based on the command history and the current input. Fish's autocompletion is also syntax highlighting, making it easier to distinguish between commands, options, and arguments.

Bash-completion Package

For those who prefer sticking with the traditional Bash shell, the bash-completion package is a community-driven project that extends the built-in completion feature. It provides a comprehensive set of scripts that enhance the completion capabilities for various commands and applications. This package is often included by default in many Linux distributions or can be easily installed from the repository.

These alternatives not only provide the basic functionality of the complete command but also bring a wealth of additional features that cater to the needs of modern Linux users.

Conclusion

The complete command in Linux is a built-in shell function that provides command-line completion; it's a handy feature that saves time and reduces errors by suggesting file names, command options, and other possibilities. The complete command is an essential tool for enhancing productivity in the Bash shell.

The complete command allows users to specify how the Bash shell should perform autocompletion for a particular command. By understanding and utilizing its options, users can tailor the shell environment to their specific needs, making command entry faster and more accurate.

Advertisements