LINFO

Command Definition


A command is an instruction given by a user telling a computer to do something, such a run a single program or a group of linked programs. Commands are generally issued by typing them in at the command line (i.e., the all-text display mode) and then pressing the ENTER key, which passes them to the shell.

A shell is a program that reads commands that are typed on a keyboard and then executes (i.e., runs) them. Shells are the most basic method for a user to interact with the system. Every Unix-like operating system has at least one shell, and most have several. The default shell on most Linux systems is bash.

A program is a sequence of instructions that is understandable by a CPU (central processing unit), the main logic unit of a computer. It indicates which operations the CPU should perform on a set of data. Programs are usually files that are stored in one of the bin directories, such as /bin, /usr/bin and /usr/local/bin.

Commands on Unix-like operating systems are either built-ins or external commands. The former are part of the shell. The latter consist of both executables, which are programs that have been written in a programming language (e.g., C, C++, Java or Python) and then compiled into a binary, and shell scripts.

A shell script is a program written in a shell programming language (e.g., bash, csh, ksh or sh) that allows users to issue a single command to execute any combination of commands, including those with options and/or arguments, together with redirection. Shell scripts are well suited for automating simple tasks and creating custom-made filters.

An option, also sometimes referred to as a switch or a flag, is a single-letter code, or sometimes a single word or set of words, that modifies the behavior of a command in some predetermined way. When multiple single-letter options are used, all the letters are placed adjacent to each other (i.e., not separated by spaces) and can be in any order. The set of options must usually be preceded by a single hyphen, again with no intervening space. An argument, also called a command line argument, is a file name or other data that is provided to a command in order for the command to use it as an input.

A command consists of a command name usually followed by one or more strings (i.e., sequences of characters) that comprise options and arguments. Each of these strings is separated by white space (which consists of one or more spaces or tabs). The general syntax for commands is

command [options] [arguments]

The square brackets indicate that the enclosed items are optional. Most commands have at least a few options and can accept (or require) arguments. However, there are some commands that do not accept arguments, and a very few with no options.

The clear command, which is used to remove all previous commands and output from the display screen, is one of the rare commands that accepts neither options nor arguments. That is, it can only be used as follows:

clear

pwd, which stands for print working directory and tells the user the name and location of the current directory (i.e., the directory in which the user is currently working), likewise does not accept any arguments. Rather, it receives its input from the shell. Moreover, it only has two very basic options, --help and --version. Thus, it is almost always used just as

pwd

The shell ignores anything that is typed on the same line after pwd, with the exception of its options, and no error messages are returned.

Other examples of commands that accept only options and no arguments are dmesg and ps. dmesg shows the messages produced by the kernel (i.e., the core of the operating system) while a system is booting up (i.e., starting up). ps shows the processes (i.e., programs in the process of execution) currently on the system.

ps is unusual in that the hyphen preceding its options is optional (at least on some versions). Because it does not accept any arguments, there is no possibility of it confusing hyphenless options with an argument. Thus, the following two commands produce identical results:

ps -aux

and

ps aux

The -a option tells ps to show all processes on the system rather than just those of the current user, the -u option tells it to show detailed information about each process and the -x option tells it to include processes that have no controlling terminal, such as daemons.

cal, whose default behavior is to display a small a calendar of the current month, is an example of a command that both has options and accepts arguments but which is often used without either.

Not all commands are restricted to the beginning of the command line. Commands that are filters can also be used following a pipe. Filters are (usually) highly specialized and small programs that transform the output of another command in a meaningful way. A pipe, which is represented by the vertical line character, is used to redirect the output from one command to become the input of another.

The following contains three commands linked with pipes:

file * | grep directory | head

The file command reports the type of each filesystem object (e.g., file, link or directory) that is supplied to it as an argument. The asterisk ( * ), which is its argument in this example, is a wildcard that stands for everything. Thus, file provides information about every object in the current directory. Its output is piped to the grep command, a filter which searches for lines of text that contain the word directory.

grep's output, in turn, is piped to the head command, whose default behavior is to take the first ten lines of text it receives and display them on the screen. Thus, this pipeline of commands displays the names and file types (i.e., directory) of the first ten directories in the current directory.

Although commands are issued in a console (i.e., an all-text display mode that occupies the entire monitor screen) or in a terminal window (i.e., an all-text mode window in a GUI that emulates a console), they can be used to launch GUI (graphical user interface) programs. The programs launched by commands can, of course, range from very simple ones, such as clear and pwd, to highly complex ones, such as the very popular Apache web server or the OpenOffice group of office programs.

There are multiple versions of some commands with minor differences in options and functionality, and the version installed by default may vary according to the operating system. Fortunately, the built-in user manual, which is accessed by using the man command followed by the name of the command as an argument, contains information relevant to the specific version on that system.

An important feature of Unix-like operating systems is the large number of standard commands that are available, as well as the numerous options for many of them, as compared with other operating systems. This is part of the Unix philosophy of providing numerous specialized commands that can easily be customized with options and combined with other commands using pipes to perform highly specific tasks that would be tedious or impractical to perform by any other means.




Created June 15, 2004. Last updated June 14, 2007.
Copyright © 2004 - 2007 The Linux Information Project. All Rights Reserved.