Command (computing): Difference between revisions

Content deleted Content added
No edit summary
Reverted 1 edit by 41.13.0.220 (talk): Edit summary doesn't correspond with edit
 
(47 intermediate revisions by 35 users not shown)
Line 1:
{{short description|DirectiveExternal instruction to a computer program}}
{{redirect distinguish|System command|system call}}
{{other uses|Command (disambiguation)#Computing{{!}}Command# § Computing}}
{{refimprove|date=May 2008 }}
In [[computing]], a '''command''' is aan directiveinstruction toreceived avia computeran programexternal to[[Interface perform(computing)|interface]] that directs the behavior of a specific[[computer taskprogram]]. ItCommonly, maycommands beare issuedsent to a program via a [[command-line interface]], such as a [[shellscripting (computing)language|shellscript]], or as input to a network service as part of a [[network protocol]], or as an event triggered in a [[graphical user interface]] triggered by the user selecting an option in a [[menu (computing)|menu]].
 
Many commands support arguments to specify input and to modify default behavior. Terminology and syntax varies but there are notable common approaches. Typically, an '''option''' or a '''flag''' is a name (without [[Whitespace character|whitespace]]) with a prefix such as [[dash]] or [[Slash (punctuation)|slash]] that modifies default behavior. An option might have a required value that follows it. Typically, flag refers to an option that does not have a following value. A '''parameter''' is an argument that specifies input to the command and its meaning is based on its position in the command line relative to other parameters; generally ignoring options. A parameter can specify anything, but often it specifies a [[file (computing)|file]] by [[filename|name]] or [[file path|path]].
Specifically, the term ''command'' is used in [[imperative programming|imperative]] [[computer language]]s. The name arises because [[statement (programming)|statements]] in these languages are usually written in a manner similar to the [[imperative mood]] used in many [[natural language]]s. If one views a statement in an [[imperative language]] as being like a sentence in a natural language, then a command is generally like a verb in such a language.
 
Many programs allow specially formatted arguments, known as flags or options, which modify the default behaviour of the program, while further [[Parameter (computer science)|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 [[object (grammar)|object]]s.
 
The term ''command'' is sometimes also used for internal program instructions, but often other terms are more appropriate such as [[statement (programming)|statement]], [[Expression (computer science)|expression]], [[Function (computer programming)|function]], or [[Conditional (computer programming)|conditional]].<ref>Maurizio Gabbrielli, Simone Martini (2010). Programming Languages - Principles and Paradigms. Springer London, ''6.3.2 Conditional Commands'', p. 140</ref> For example, printing a message in Bash is via the ''command'' [[Printf (Unix)|printf]], while in Python it is via the ''function'' print().<ref>{{cite web | url = https://docs.python.org/3/library/functions.html#print | access-date = 23 October 2023 | title = Built-in Functions - print | publisher = python.org }}</ref> Further, some aspects of adjacent technology are conflated with commands. For example, conditional logic in Bash and Python is called an ''expression''<ref>{{cite web | url = https://docs.python.org/3/reference/expressions.html | access-date = 23 October 2023 | title = Conditional expressions | publisher = python.org }}</ref><ref>{{cite web | url = https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html | access-date = 23 October 2023 | title = Bash Conditional expressions | publisher = gnu.org }}</ref> and ''statements'' in Java.<ref>{{cite web | url = https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html | access-date = 23 October 2023 | title = The if-then and if-then-else Statements | publisher = oracle.com }}</ref>
 
==Examples==
Here are some commands given to a [[command-line interpreter]] ([[Unix shell]]).
 
A notable context in which commands are prevalent is the [[operating system]] [[shell (computing)|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 (Unix shell)|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 (command)|cd]]'':
The following command changes the user's working position in the [[directory tree]] to the directory ''/home/pete''. The utility program is ''[[Cd (command)|cd]]'' and the argument is ''/home/pete'':
<syntaxhighlight lang="bash">
cd /home/pete
</syntaxhighlight>
 
The following bash command printswrites the text ''"Hello World''" onvia theprogram [[standardEcho output(command)|echo]] stream,to which,[[standard inoutput]] this{{endash}} case, just printstypically the text on the screen. The program name is [[Echocomputer (command)terminal|echoterminal]] and the argument is "Hello World". TheQuotes quotesaround arethe usedtwo towords preventindicate ''Hello''that andthe ''World''phrase beingis treated as separatea tokens:single parameter.
echo "Hello World"
 
<syntaxhighlight lang="bash">
The following commands are equivalent. They list files in the directory ''/bin''. The program is ''[[ls]]'', having three flags (''l'', ''t'', ''r''), and the argument is the directory ''[[Filesystem Hierarchy Standard|/bin]]'':
echo "Hello World"
ls -l -t -r /bin
</syntaxhighlight>
ls -ltr /bin
 
The following demonstrates how the default behavior of a command is modified with a switch. The switch {{code|-e}} causes the command to treat characters prefixed with a backslash as the associated control character. In this case {{code|\t}} results in a tab character.
The following command displays the contents of the files ''ch1.txt'' and ''ch2.txt''. The program name is ''[[cat (Unix)|cat]]'', having two file name arguments:
cat ch1.txt ch2.txt
 
<syntaxhighlight lang="bash">
Here are some commands for the [[DOS]], [[OS/2]] and [[Microsoft Windows]] [[Cmd.exe|command prompt]] processor. The following command displays the contents of the file ''readme.txt''. The program name is ''[[TYPE (DOS command)|type]]'' and the argument is ''readme.txt''.<ref>{{cite web | url = https://ss64.com/nt/type.html | access-date = 14 March 2019 | title = Type - Display a text file - Windows CMD | publisher = SS64.com }}</ref>
echo -e "Hello\tWorld"
type readme.txt
</syntaxhighlight>
 
TheIn followingshells such as [[Cmd.exe|command listsprompt]], the[[DOS]], contentsand of[[OS/2]] thesome currentcommands directory.are Thebuilt-in; are not implemented as a separate program. nameBut, isif ''[[Dira (command)|dir]]'' is not built-in, andthen the shell dispatches to a program that has an executable extension (such as ''Q.exe'') isand abase flagname requestingmatching thatthe first parameter ignoring case. The following command prompt command displays the ownercontent of each file also''readme.txt'' bevia listedthe built-in command ''[[TYPE (DOS command)|type]]''.<ref>{{cite web | url = https://ss64.com/nt/dirtype.html | access-date = 14 March 2019 | title = DIRType - listDisplay filesa andtext foldersfile - Windows CMD | publisher = SS64.com }}</ref>
 
dir /Q
<syntaxhighlight lang="batch">
type readme.txt
</syntaxhighlight>
 
HereThe arefollowing some commands for the [[DOS]], [[OS/2]] and [[Microsoft Windows]] [[Cmd.exe|command prompt]] processor. The following command displayslists the contents of the filecurrent ''readme.txt''.directory Thevia programbuilt-in name iscommand ''[[TYPEDir (DOS command)|typedir]]''. andThe the argument isswitch ''readme.txt/Q'' modifies default behavior to include owner information.<ref>{{cite web | url = https://ss64.com/nt/typedir.html | access-date = 14 March 2019 | title = TypeDIR - Displaylist afiles textand filefolders - Windows CMD | publisher = SS64.com }}</ref>
 
<syntaxhighlight lang="batch">
dir /Q
</syntaxhighlight>
 
== See also ==
* [[Formal grammar]]
* [[Gesture recognition]]
* [[List of UnixPOSIX commands]]
* [[List of DOS commands]]
* [[Formal grammar]]
 
== References ==