In Linux, while typing a command if you press TAB twice, it would list all available commands that starts with typed characters.
This is nothing new, probably you already know about this. This
functionality is called bash completion. The basic file and directory
name completion are available by default in bash command line.
But, we can turbo-charge this bash completion, and take it to the next level using complete command.
This tutorial explains how we can apply the auto-completion to options and to command’s arguments using programmable completion.
For example, after typing write command, if you press tab twice,
auto-completion provides list of users to perform write the operation.
In the following example, it would show available hostnames for the telnet command:
To get programmable completion in your terminal, you just need to run /etc/bash_completion as shown below,
You can also uncomment the below lines in /etc/bash.bashrc(from
ubuntu linux 13.04) so that you dont have to run above command
explicitly,
In case if you don’t find these lines and /etc/bash_completion file,
then you just need to install the package called bash_completion by apt-get
After enabling programmable bash completion, there are set of bash
completion defined. The command complete is used for defining bash
completion.
To view the existing bash completion, use the complete command as shown below.
Option -p is optional in the above example.
Bash provides the following standard completion for the Linux users by default.
Variablename completion
Username completion
Hostname completion
Pathname completion
Filename completion
We discussed about these in our earlier bash standard completion article.
Define a completion with -c command to get the list of available
command as argument. In the following example, the completion is defined
for which command,
As seen above, by pressing ‘y’, all commands will get listed.
With option d, the completion can be defined to get only directory
names as argument. In the following example, defined completion for ls,
As seen above, pressing tab only shows you directories.
With complete, it is also possible to get job names as argument to
commands. Option j is used to pass job names as argument to command job
as shown below,
Talking about background jobs, you should also know how to manage Linux background jobs using these examples.
The completions can be defined with desired prefix to be added and
suffix to be append with actual completions. In the following example,
prefix and suffix is defined for list_job_attrib.sh,
Consider that script completes its run, output got written to a output directory as follows
In the above, if you need to ignore the .tmp and .o files for the auto-completion with ls command then,
FIGNORE is the shell variable that contains suffix of filenames and those gets excluded in the auto-completion.
The wordlist can be mentioned with -W option and that gets splited
with the value in IFS variable. Then each resultant word is expanded and
would be displayed for completion,
As stated above, after splited the string by IFS delimeter, the word
get expanded, so its also possible to have these as variables as shown
below,
It allows you to include a function to define completion. With -F
option, the function name passed to complete command and it gets
executed to generate completions. For example, the functions is written
where in the above function,
COMPREPLY : array holds the completion results that gets displayed after pressing [TAB][TAB]
COMP_WORDS : array of words that is typed on the command line
COMP_CWORD : index for the COMP_WORDS array and using this different position words on command line can be accessed.
compgen : -W holds the possible completions and the respective argument get chosen based on the $current_arg
This function present in file parser_option gets sourced as shown below,
Link this function to your parser script as shown below,
As seen above, the options for parsers gets generated by function _parser_options().
Note : Look at /etc/bash_completion to view more functions for programmable completion.
If there is no matches generated by the defined completion
specification, then comp-option is being taken for completion mentioned
with -o option.
As above, the completion is defined with _count_files function for
file ./countfiles.sh. If the _count_files() function doesnt generate any
match then directory completion gets attempted.