Open In App

Shell Scripting – Complete Command

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Suggest directories only

 

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

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:

Shows files with .fizz extension

 

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads