NEXT UP previous
Next: Shell Functions

Command Completion

Another useful feature of bash is its ability to attempt to complete your command lines for you. At any time during the entry of a command you can press the Tab key. When you do this, the shell will attempt to complete whatever part of your command you were typing at the time. For example, suppose you wanted to change your password with the passwd command; you could enter the following (the notation <Tab> means press the Tab key):

	$ pass<Tab>

When you enter this character sequence, bash knows you are entering a command name and it will search for a command that begins with the characters you typed before you pressed the Tab key (pass in this example). The only command that the shell can find starting with these characters is passwd and so it completes the command name for you by adding the extra characters onto the end of what you have already typed.

If the number of characters you type is insufficient for bash to be able uniquely to identify what command it should use, then you will get an audible warning. Pressing the Tab key a second time at this point will cause bash to display a list of all the possible ways the command could be completed:

	$ pass<Tab><Tab< 
	passwd paste

You can then add enough characters to your command line to enable the shell to resolve the ambiguity next time you press the Tab key.

In addition to being able to complete command names in this way, the shell can also complete file names for you, when you use them as command parameters:

	$ tail -2 /etc/p<Tab><Tab>
	passwd  printcap  profile
	$ tail -2 /etc/pa<Tab>
	pc :fjKppCZxEvouc:500:500:: /usrl/pc:/bin/bash
	carey:Yt1a4ffkG2r02:501:500:: /usr1/carey: /bin/bash

Here, we are trying to display the last two lines in the password file. The shell was unable to resolve the file name on the first attempt, using the file name /etc/p<Tab>. Pressing <Tab> for a second time displays the three choices passwd, printcap and profile in the directory /etc, because they all start with the letter p. As you can see from this list, adding one extra character (an a in this case) will resolve the ambiguity for the shell, and correctly result in the required command line being executed.


NEXT UP previous
Next: Shell Functions