Jump to content

Command (computing)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Stevebroshar (talk | contribs) at 20:44, 2 April 2025 (See also: Match actual name of target page; alphabetize). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computing, a command is an instruction received via an external interface that directs the behavior of a computer program. Commonly, commands are sent to a program via a command-line interface, a script, a network protocol, or as an event triggered in a graphical user interface.

Specifically, the term command is used in imperative programming languages. The name arises because statements in these languages are usually written in a manner similar to the imperative mood used in many natural languages. A statement in an imperative programming language would then be a sentence in a natural language, and the command would be the predicate.

Many programs allow specifically formatted arguments, known as flags or options, which modify the default behaviour of the program, while further arguments may provide objects, such as files, to act on. As an analogy to a natural language, the flags are adverbs, while the other arguments are grammatical objects.

Distinction between command and expression, statement and function

The meaning of command is highly dependent on context. For example, some authors refer to conditionals as commands [1] while they are called expressions in Python[2] or Bash[3] and statements in Java.[4] Similarly, writing to stdout is done in Bash with the builtin command printf, while it is done with the built-in function print() in Python.[5]

Examples

A notable context in which commands are prevalent is the operating system shell. Commonly, the shell dispatches a command to a program that has a file name matching the first parameter. In a Unix shell (such as bash and many related variants), the match must be exact including case. The following bash command changes the working directory to /home/pete by invoking the program cd:

cd /home/pete

The following bash command writes "Hello World" via program echo to standard output – typically the terminal. Quotes around the two words indicate that the phrase is treated as a single parameter.

echo "Hello World"

The following demonstrates how the default behavior of a command is modified with a switch. The switch -e causes the command to treat characters prefixed with a backslash as the associated control character. In this case \t results in a tab character.

echo -e "Hello\tWorld"

In shells such as command prompt, DOS, and OS/2 some commands are built-in; are not implemented as a separate program. But, if a command is not built-in, then the shell dispatches to a program that has an executable extension (such as .exe) and base name matching the first parameter ignoring case. The following command displays the content of file readme.txt via the built-in command type.[6]

type readme.txt

The following command lists the contents of the current directory via built-in command dir. The switch /Q modifies default behavior to include owner information.[7]

dir /Q

See also

References

  1. ^ Maurizio Gabbrielli, Simone Martini (2010). Programming Languages - Principles and Paradigms. Springer London, 6.3.2 Conditional Commands, p. 140
  2. ^ "Conditional expressions". python.org. Retrieved 23 October 2023.
  3. ^ "Bash Conditional expressions". gnu.org. Retrieved 23 October 2023.
  4. ^ "The if-then and if-then-else Statements". oracle.com. Retrieved 23 October 2023.
  5. ^ "Built-in Functions - print". python.org. Retrieved 23 October 2023.
  6. ^ "Type - Display a text file - Windows CMD". SS64.com. Retrieved 14 March 2019.
  7. ^ "DIR - list files and folders - Windows CMD". SS64.com. Retrieved 14 March 2019.