Command (computing): Difference between revisions

Content deleted Content added
add.
Tags: Reverted Visual edit Mobile edit Mobile web edit
Reverted 1 edit by 41.13.0.220 (talk): Edit summary doesn't correspond with edit
 
(27 intermediate revisions by 22 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 an instruction received via an external [[Interface (computing)|interface]] that directs the behavior of a [[computer program]]. Commonly, commands are sent to a program via a [[command-line interface]], a [[scripting language|script]], a [[network protocol]], or as an event triggered in a [[graphical user interface]].
In [[computing]], a '''command''' or '''programming commands'''<ref>{{Cite web |last=Staff |first=Webopedia |date=1996-09-01 |title=What is Instruction? |url=https://www.webopedia.com/definitions/instruction/ |access-date=2023-01-23 |website=Webopedia |language=en-US}}</ref>''','''
 
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]].
is a directive to a computer program to perform a specific task. It may be issued via a [[command-line interface]], such as a [[shell (computing)|shell]], or as input to a network service as part of a [[network protocol]], or as an event in a [[graphical user interface]] triggered by the user selecting an option in a [[menu (computing)|menu]].
 
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>
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.
 
==Examples==
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.
 
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]]'':
<syntaxhighlight lang="bash">
cd /home/pete
</syntaxhighlight>
 
The following bash command writes "Hello World" via program [[Echo (command)|echo]] to [[standard output]] {{endash}} typically the [[computer terminal|terminal]]. Quotes around the two words indicate that the phrase is treated as a single parameter.
==Examples==
Here are some commands given to a [[command-line interpreter]] ([[Unix shell]]).
 
<syntaxhighlight lang="bash">
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'':
echo "Hello World"
cd /home/pete
</syntaxhighlight>
 
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 prints the text ''Hello World'' on the [[standard output]] stream, which, in this case, just prints the text on the screen. The program name is [[Echo (command)|echo]] and the argument is "Hello World". The quotes are used to prevent ''Hello'' and ''World'' being treated as separate tokens:
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 -e "Hello\tWorld"
ls -l -t -r /bin
</syntaxhighlight>
ls -ltr /bin
 
HereIn areshells somesuch commandsas for[[Cmd.exe|command theprompt]], [[DOS]], and [[OS/2]] andsome [[Microsoftcommands Windows]]are [[Cmdbuilt-in; are not implemented as a separate program.exe| But, if a command prompt]]is processornot 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 prompt command displays the contentscontent of the file ''readme.txt''. Thevia programthe namebuilt-in iscommand ''[[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>
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="batch">
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>
type readme.txt
</syntaxhighlight>
 
The following command prompt command lists the contents of the current directory. Thevia programbuilt-in name iscommand ''[[Dir (command)|dir]]'',. andThe switch ''/Q'' ismodifies adefault flagbehavior requestingto that theinclude owner of each file also be listedinformation.<ref>{{cite web | url = https://ss64.com/nt/dir.html | access-date = 14 March 2019 | title = DIR - list files and folders - Windows CMD | publisher = SS64.com }}</ref>
 
dir /Q
<syntaxhighlight lang="batch">
dir /Q
</syntaxhighlight>
 
== See also ==
* [[Formal grammar]]
* [[Gesture recognition]]
* [[List of UnixPOSIX commands]]
* [[List of DOS commands]]
* [[Formal grammar]]
 
== References ==