1.15 Command Completion

A useful tool of the bash shell is the ability to automatically complete commands and their arguments. Like many command line shells, bash offers command line completion, where you type a few characters of a command (or its filename argument) and then press the Tab key. The bash shell will complete the command (or its filename argument) automatically for you. For example, if you type ech and press Tab, then the shell will automatically complete the command echo for you.

There will be times when you type a character or two and press the Tab key, only to discover that bash does not automatically complete the command. This will happen when you haven’t typed enough characters to match only one command. However, pressing the Tab key a second time in this situation will display the possible completions (possible commands) available.

A good example of this would be if you typed ca and pressed Tab, then nothing would be displayed. If you pressed Tab a second time, then the possible ways to complete a command starting with ca would be shown:

sysadmin@localhost:~$ ca
cal capsh cat cautious-launcher
calendar captoinfo catchsegv
caller case catman
sysadmin@localhost:~$ ca

Another possibility may occur when you have typed too little to match a single command name uniquely. If there are more possible matches to what you’ve typed than can easily be displayed, then the system will ask you if you want to display all possibilities.

For example, if you just type c and press the Tab key twice, the system may provide you with a prompt like:

Display all 102 possibilities? (y or n)

You should probably respond with n in a situation like this and then continue to type more characters to achieve a more refined match.

A common mistake when typing commands is to misspell the command. Not only will you type commands faster, but you will type more accurately if you use command completion. Using the Tab key to automatically complete the command helps to ensure that the command is typed correctly.

Note that completion also works for arguments to commands when the arguments are file or directory names.