In Linux, enter a command and press the TAB key twice to list all available commands starting with the input character. This is not new. You probably already know this. This function is called command completion. By default, the bash command line can automatically complete the file or directory name. However, we can make the bash command line complete to execute more operations, and the completion command can make it the next brilliant.
This tutorial illustrates how to use the programmable completion function to apply the auto-completion function to options or command line parameters.
For example, if you press the TAB twice after entering the write command, the auto-completion function provides a list of write operations.
$ Write [TAB] [TAB]
Bala AJ
Jason randy
John ritu
Mayla thomas
Nisha www-data
In the following example, enter the telnet command to display the available host names:
$ Telnet [TAB] [TAB]
Localhost dev-db fileserver
To enable the programmable command complementing function to work on your terminal, you only need to execute/etc/bash_completion, as shown below:
#./Etc/bash_completion
You can also cancel the comments under/etc/bash. bashrc (for Ubuntu Linux 13.04) so that you do not need to execute the above command,
Enable bash completion in interactive shells
If! Shopt-oq posix; then
If [-f/usr/share/bash-completion/bash_completion]; then
./Usr/share/bash-completion/bash_completion
Elif [-f/etc/bash_completion]; then
./Etc/bash_completion
Fi
Fi
If you do not find the code or the/etc/bash_completion file, you only need to install the bash_completion package by using the apt-get command.
1. view the existing bash completion command
By enabling the programmable bash command line complementing function, you can define a set of bash completion commands. Command Line completion can be used to define bash completion commands.
Let's take a look at the existing bash complementing function and use the complete command as follows:
Complete-p | less
Option-p is optional.
2. List of standard completions in Bash
Bash provides the following standard completion commands for linux users by default.
- Variable name completion (Variablename completion)
- Username completion)
- Host Name completion)
- Pathname completion)
- Filename completion)
3. Define completion commands for obtaining commands
Use the-c parameter to define a completion command to obtain the list of available commands. In the following example, a completion command is defined for the which command,
$ Complete-c which
$ Which [TAB] [TAB]
Display all 2116 possibilities? (Y or n)
As shown above, if you press "y", all commands will be displayed.
4. Define the completion command for obtaining the Directory
Use the parameter d to define a completion command that only obtains the directory name. In the following example, the completion command of ls is defined.
$ Ls
Countfiles. sh dir1/dir2/dir3/
$ Complete-d ls
$ Ls [TAB] [TAB]
Dir1/dir2/dir3/
As shown above, you can view the directory name by pressing the TAB twice in a row.
5. Obtain the completion command for obtaining the background job name
You can use the complete command to obtain the job name as a parameter. The parameter j is used to pass the job name as a parameter to the command line, as shown below:
$ Jobs
[1]-Stopped cat
[2] + Stopped sed 'P'
$ Complete-j./list_job_attrib.sh
$./List_job_attrib.sh [TAB] [TAB]
Cat sed