Command Completion and How to Use It

Command completion is the automatic completion by the shell of the names of commands and their arguments that have only partially been typed into the command line.

It is a sophisticated and extremely convenient feature of Unix-like operating systems that takes much of the drudgery out of using the command line, and it is one of the numerous features that helps make such operating systems far more convenient and powerful than other types of command line operating systems, such as MS-DOS.

A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems. Its primary function is to read commands that are typed into the command line and then execute (i.e., run) them. A command is an instruction telling a computer to do something. It consists of the basic command, which is the name of an executable file (i.e., a ready-to-run) program along with any options and arguments, and it is run by typing them into the command line and then pressing the ENTER key. This passes it to the shell, which runs the corresponding program(s). An argument is input information for a command. The command line is the all-text display mode.

Command completion works for arguments that are the names of filesystem objects, i.e., files, directories and links. It does not work for arguments that contain other types of information (e.g., numbers or names of users). Likewise, it does not work for options, which are standardized single or multiple letter codes that modify the behavior of a command in some predefined way.

Command completion is invoked by pressing the TAB key after an appropriate name has been partially typed in. If the typed-in string (i.e., sequence of characters) is sufficient to uniquely identify an object in the relevant directory, the remainder of the name is then added automatically to the command line.

If an insufficient number of characters have been typed in to uniquely identify a filesystem object and the TAB key is pressed once, nothing happens. However, if the TAB key is pressed a second time, a list of all appropriate items whose names begin with the string that the user typed in is displayed. This makes it convenient for the user to find the appropriate item and again begin typing, and use command completion again if convenient.

As a simple example, if it is desired to use the head command (which by default displays the first ten lines of any files provided as arguments), the user might type in the first two letters, i.e., h and e, and then press the TAB key. However, this alone is not sufficient to uniquely identify head, as several other commands also exist on a typical system that also start with the string he. A full list of these commands can be seen by pressing the TAB key a second time. Typing the next letter, i.e., a, is sufficient to uniquely identify the head command to the shell and thus to allow the rest of the command name to be filled in automatically.

Command completion is particularly useful in the case of commands, files and directories with long names. Moreover, the number of characters that needs to be typed in order for it to work can be as few as one, even with the longest names, if a single character is sufficient to uniquely identify the object.

Thus, for example, if it is desired to read the first ten lines of the file named yellowstone_unique_species_list and there is only one file in the current directory that begins with the letter y, then all that is necessary is to type head y and press the TAB key. The command (and its argument) head yellowstone_unique_species_list will then be completed by the shell and written on the display screen. All that is necessary for the user to do in order to execute the command is to push the ENTER key.

However, were there also additional files in the same directory beginning with the same letter, then it would be necessary to type in more than just a single letter. For example, if there were additional files beginning with y, but not with ye, such as yarrow_bay_unique_species_list, then it would be necessary to type the first two letters, i.e., ye, in order to get command completion to finish the typing.

Command completion can work anywhere within a name being typed, and thus it can also be very useful for situations in which names are identical except for their final parts. For example, if there were three files in the current directory beginning with the letter y and they were yarrow_bay_unique_species_list_first, yarrow_bay_unique_species_list_second and yarrow_bay_unique_species_list_third, then command completion would fill in all of the typing after the initial y and until a unique string (i.e., first, second or third) was reached. That is, it would fill in yarrow_bay_unique_species_list_, and the user could then type in the final string.

However, in addition to working anywhere in a name, command completion can be used used multiple times within a single name. Thus, in the above example, it would not be necessary for the user to type in the full word first, second or third. That is, after the shell has entered yarrow_bay_unique_species_list_ from the first command completion, the user could employ command completion again by just entering the letter f, s or t and then pressing the TAB key.

The shell attempts to be intelligent about command completion. This includes context sensitivity and first attempting to interpret special characters as special characters rather than as ordinary characters. For example, if the typed string begins with the dollar sign character ( $ ), the shell tries to complete it as a shell variable (because the dollar sign is commonly used to represent a shell variable) rather than as merely a dollar sign. Likewise, if the typed in string starts with an at symbol ( @ ), the shell attempts to complete it as a host name (i.e., computer name on the network). Also, when appropriate, the shell tries completing typed in strings as aliases (i.e., substitute or simplified names for commands or groups of commands). If none of the above works, the shell then tries to complete the string as the name of a file, directory or link.

In addition to being able to function anywhere and multiple times within a single filesystem object name being typed, command completion can also function anywhere and any number of times within a line being typed. That is, it can be used for each command name and for each of their arguments that is a filesystem object name in a line, including for commands that are connected by pipes (which are represented by the vertical bar character) and commands which are not connected but are merely listed on the same line and separated by a semicolon.

Thus, for example, command completion could be used with bin, pwd, ls, home, jane, accounts, jones.txt, sort and head to simplify entry of the following line of input into the command line:

/bin/pwd; ls -als /home/jane/accounts/jones.txt | sort -r | head

Command completion's context sensitivity prevents it from completing the names of files that are not executable files (i.e., ready-to-run commands) and the names of directories that are not part of the full pathname (i.e., the location of a filesystem object relative to the root directory) of executable files unless such files and directories follow the name of an executable file (i.e., a command name) and are thus its arguments.

A useful by-product of command completion is that it can help indicate whether the names of files, directories and commands are being typed correctly. Thus, for example, if a user wants to use the apropos command but types in appr or Apro and then presses the TAB key, nothing will happen, thus indicating that the command is spelled incorrectly, not installed on the system, or not in the user's PATH variable (i.e., the series of directories in which the shell searches for a command). If the user is not sure of the spelling, a wise move might be to just enter the string ap and then press the TAB key twice to see a list of all commands beginning with that string.

Another user friendly, although minor, feature of command completion is that after pressing the tab key twice to list the names of all relevant objects beginning with a particular string, that string is restored on a subsequent line along with the command prompt, thereby relieving the user of the need to retype it.

Command completion is supported by most modern shells in Unix-like operating systems including bash (the default shell for Linux), csh (the C shell), sh (the Bourne Shell) and tcsh (the TENEX C shell). However, it is not supported by ash (the Almquist shell), a minimalist clone of sh that features small memory space requirements.

Created August 4, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.