Shell Scripting - Complete Command
Last Updated :
02 Jan, 2023
The complete command is used to autocomplete commands while typing by pressing the [TAB] key. In case there is more than one possible autocompletion, pressing the [TAB] key lists all of them. The complete command is not a separate package rather it's a built-in command for the bash shell. It handles the autocomplete functionality while typing commands and pressing tabs.
It is what lists possible subfolders when you type:
cd <TAB><TAB>
Syntax of Complete Command:
complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
The complete command can be used to specify how to perform auto-completion by the deadline. Arguments have to be specified for each Name given.
In case no options are given the command will print out all of the existing completions.
Flags used:
- -p: prints the existing completions
- -r: will remove completion for the given name OR remove all completions if no specific name is provided
- -D: use the default completions and actions for commands that don't have a defined completion
- -E: autocompletes empty commands OR completions on blank lines
Usage of Complete Command
Example 1: Suggest directories only
It is possible to define the -d flag suggestions to only accept directory names as arguments. Here we define the ls command to suggest only directories
Command:
ls
complete -d ls
ls
Output:
Notice the last line only generates directories as suggestions.
Example 2: Create a Function of Your Own to Produce Completion
Say you had a command or a script which worked only with a certain type of file. So you only want to list those files whenever you press <TAB><TAB> after your command.
This is not the default behavior, by default complete will recommend all types of files to you, in order to change that we can customize the complete command for our command or script.
Command:
touch fizz
chmod +x fizz
touch a.fizz a.buzz b.fizz b.buzz
ls
fizz
Output:
Firstly we created a dummy file called fizz. Next, we made it an executable file. Then we created some dummy files. Now writing fizz and using double tabs gives us a list of all files
The problem is that our program works only with fizz files so we don't need these extra suggestions.
Now to customize the complete command for our executable
Command:
complete -f -X '!*.fizz' fizz
fizz
Output:
This command here tells bash that autocompletes for fizz works by getting a list of all the files and then excluding the ones that do not match the pattern of *.fizz it only shows the files with the .fizz extension
A user-defined function may also be provided to the complete command for better control over the autocompletes and to handle more complex cases. The function can be defined in your .profile file.
complete -F _FUNCTION_ Fizz
Similar Reads
Shell Scripting - Set Command The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Scripting - True Command A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c
3 min read
Shell Scripting - Shopt Command Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Basic Shell Commands in Linux: Complete List Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively.The Linux shell serves as an interface for us
5 min read
npm completion command The npm completion command provides a way to enable shell completion for npm commands, making it easier to use the npm CLI by suggesting commands and arguments as you type. This can save time and reduce the risk of making typos, especially when dealing with long command names or package names.Prereq
3 min read