How a command line is interpreted

suggest change

The parsing of a command line into a sequence of commands is complex, and varies subtly from command interpreter to command interpreter. There are, however, four main components:

Variable substitution

A command line is scanned for variable specifications, and any found are replaced with the contents of those variables.

Quoting

`Special characters can be quoted, to remove their special meanings.

Syntax

Command lines are developed into a sequence of commands according to a syntax.

Redirection

Redirection specifications are applied, and removed from the command line, before an individual command in a sequence is executed.

Variable substitution

Command lines can contain variable specifications. These comprise a % character followed by a name, followed by a second % character unless the name is a digit in 0 ... 9 or an asterisk *.

Variable specifications are replaced with values as follows:

Special names

Some variable names are not visible using SET command. Rather, they are made available for reading using the % notation. To find out about them, type help set.

Special variable names and what they expand to:

Name Replacement Value Used
%CD% The current directory, not ending in a slash character if it is not the root directory of the current drive
%TIME% The system time in HH:MM:SS.mm format.
%DATE% The system date in a format specific to localization.
%RANDOM% A generated pseudo-random number between 0 and 32767.
%ERRORLEVEL% The error level returned by the last executed command, or by the last called batch script.
%CMDEXTVERSION% The version number of the Command Processor Extensions currently used by cmd.exe.
%CMDCMDLINE% The content of the command line used when the current cmd.exe was started.

Links:

Quoting and escaping

You can prevent the special characters that control command syntax from having their special meanings as follows, except for the percent sign (%):

The special characters that need quoting or escaping are usually <, >, |, &, and ^. In some circumstances, ! and \ may need to be escaped. A newline can be escaped using caret as well.

When you surround the string using quotation marks, they become part of the argument passed to the command invoked. By contrast, when you use caret as an escape character, the caret does not become part of the argument passed.

The percent sign (%) is a special case. On the command line, it does not need quoting or escaping unless two of them are used to indicate a variable, such as %OS%. But in a batch file, you have to use a double percent sign (%%) to yield a single percent sign (%). Enclosing the percent sign in quotation marks or preceding it with caret does not work.

Examples

Links:

Syntax

Command lines are developed into a sequence of commands according to a syntax. In that syntax, simple commandsmay be combined to form pipelines, which may in turn be combined to form compound commands, which finally may be turned into parenthesized commands.

A simple command is just a command name, a command tail, and some redirection specifications. An example of a simple command is dir *.txt > somefile.

A pipeline is several simple commands joined together with the "pipe" metacharacter — |, also known as the "vertical bar". The standard output of the simple command preceding each vertical bar is connected to the standard input of the simple command following it, via a pipe. The command interpreter runs all of the simple commands in the pipeline in parallel. An example of a pipeline (comprising two simple commands) is dir *.txt | more.

A compound command is a set of pipelines separated by conjunctions. The pipelines are executed sequentially, one after the other, and the conjunction controls whether the command interpreter executes the next pipeline or not. An example of a compound command (comprising two pipelines, which themselves are just simple commands) is move file.txt file.bak && dir > file.txt.

The conjunctions:

A parenthesized command is a compound command enclosed in parentheses (i.e. ( and )). From the point of view of syntax, this turns a compound command into a simple command, whose overall output can be redirected.

For example: The command line ( pushd temp & dir & popd ) > somefile.txt causes the standard output of the entire compound command ( pushd temp & dir & popd ) to be redirected to somefile.txt.

Links:

Redirection

Redirection specifications are applied, and removed from the command line, before an individual command in a sequence is executed. Redirection specifications control where the standard input, standard output, and standard error file handles for a simple command point. They override any effects to those file handles that may have resulted from pipelining. (See the preceding section on command syntax.) Redirection signs > and >> can be prefixed with 1 for the standard output (same as no prefix) or 2 for the standard error.

The redirection specifications are:

< filename Redirect standard input to read from the named file.

> filename Redirect standard output to write to the named file, overwriting its previous contents.

>> filename Redirect standard output to write to the named file, appending to the end of its previous contents.

>&h Redirect to handle h, where handle is any of 0—standard input, 1—standard output, 2—standard error, and more.

<&h Redirect from handle h.

Examples:

Links:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents