10 examples of command line completion in Linux
In Linux, when you enter a command, pressTAB
To list all available commands starting with your input. This is not new. You may already know it. This function is called command line completion bash completion. By default, the bash command line can automatically complete the file or directory name. However, we can enhance the bash command complementing function throughcomplete
Command to bring it to a new height.
This tutorial illustrates how to use the programmable completion function to apply the Automatic completion function to options or command line parameters.
For examplewrite
After the command, if you pressTAB
Press the button. The Automatic completion function is available for execution.write
List of users to operate on.
$ write[TAB][TAB]
bala raj
jason randy
john ritu
mayla thomas
nisha www-data
In the following exampletelnet
Command to display available host names:
$ telnet [TAB][TAB]
localhost dev-db fileserver
To enable the Programme command complementing function to work on your terminal, you only need to execute the following/etc/bash_completion
You can:
#./etc/bash_completion
You can also cancel/etc/bash.bashrc
(From Ubuntu Linux 13.04). In this way, 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/etc/bash_completion
File, you only need to useapt-get
Command to install the bash_completion package.
1. view existing command line completion
After the programmable command line complementing function is enabled, some defined command completion functions are available.complete
Command is used to define command line completion.
To view the existing command line completion, usecomplete
Command:
complete -p |less
The-p option in the preceding example is optional.
2. List the standard complementing functions in bash
By default, Bash provides the following standard complementing functions for Linux users.
- Variable completion
- Username completion
- Host Name completion
- Path completion
- File Name completion
We have discussed this in the previous bash standard completion.
3. Define a command name to complete
The-c option can be used to complete all available commands as a command. In the following examplewhich
The command defines a completion (LCTT)TAB
All command names can be listed as parameters that can be supplemented ).
$ complete -c which
$ which [TAB][TAB]
Display all 2116 possibilities?(y or n)
If you press 'y', all command names are listed.
4. Define a directory completion
You can use option-d to define a complete parameter that only contains the directory name. In the following examplels
Command defines completion.
$ ls
countfiles.sh dir1/ dir2/ dir3/
$ complete -d ls
$ ls[TAB][TAB]
dir1/ dir2/ dir3/
As above, pressTAB
Only the directory name is displayed.
5. Define a background Task Name to complete
The completion function can also use the task name as the completion parameter. Option-j defines the task name as a parameter passed to the command, as follows:
$ jobs
[1]-Stoppedcat
[2]+Stoppedsed'p'
$ complete -j ./list_job_attrib.sh
$ ./list_job_attrib.sh [TAB][TAB]
catsed
For background tasks, see examples in Linux background tasks to learn how to manage background tasks.
6. completion with a prefix and suffix
The completion function defines the prefix and suffix for the actual completion content. In the following example, the prefix and suffix of the supplemented content are defined for list_job_attrib.sh.
$ jobs
[1]+Stoppedcat
$ complete -P '">'-S '<"'./list_job_attrib.sh
$ ./list_job_attrib.sh [TAB][TAB]
$ ./list_job_attrib.sh ">cat<"
7. Complete the excluded file names and directory names
After the script is run, the output directory is as follows:
$ cd output/
$ ls
all_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt
parser_mod.tmp extract.o
If you wantls
Command completion to ignore the. tmp and. o files:
$ export FIGNORE='.tmp:.o'
$ complete -f -d ls
$ cd output
$ ls[TAB][TAB]
all_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt
FIGNORE
Is an environment variable that contains the filename suffixes to be excluded for Automatic completion.
8. Use the IFS Variable to split the string to obtain the completion value.
You can use the-W option to define the completion value list, and then useIFS
Split environment variables. The splitting result is displayed as a variable and displayed as a completion.
$ export IFS=" "
$ complete -W "bubble quick"./sort_numbers.sh
$ ./sort_numbers.sh [TAB][TAB]
bubble quick
As described aboveIFS
After the Delimiter is split, the embedded variables are expanded as variable values, so you can use the following variables:
$ echo $SORT_TYPE1
bubble
$ echo $SORT_TYPE2
quick
$ complete -W "$SORT_TYPE1 $SORT_TYPE2"./sort_numbers.sh
$ ./sort_numbers.sh [TAB][TAB]
bubble quick
9. Write a function to generate completion
You can introduce a function to define completion. Use the-F option to pass the function namecomplete
Command to execute the function to generate the completion content. For example, the function is as follows:
_parser_options()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W '-i --incoming -o --outgoing -m --missed'-- $curr_arg ));
}
In the above functions:
- COMPREPLY
TAB
Result
- COMP_WORDS: the array contains the words entered by the command line.
- COMP_CWORD: The index of the COMP_WORDS array. It is used to distinguish the word locations that can be accessed by the command line.
- Compgen:-W provides possible completion and its parameters based on $ current_arg
This function is stored in the parser_option file and passedsource
Command introduction:
$ source parser_option
Associate this function with your parser. pl script:
$ complete -F _parser_options ./parser.pl
$ ./parser.pl [TAB][TAB]
-i --incoming -o --outgoing -m --missed
As shown above, the parser. pl option is generated by the function _ parser_options.
Tip: View/etc/bash_completion
To learn more about programmable completion functions.
10. When the first rule does not generate a result, the second rule is used.
If the defined completion rule does not generate a match, you can use the-o option to generate a completion rule.
$ complete -F _count_files -o dirnames ./countfiles.sh
As shown above, the _ count_files completion function is defined for./countfiles. sh. If the _ count_files () function does not generate any match, directory completion is triggered.
$ ls
countfiles.sh dir1/ dir2/ dir3/
$./countfiles.sh [TAB][TAB]
dir1 dir2 dir3
This article permanently updates the link address: