[Toc][Index]

Argument Quoting


As it parses the command line, CMD.EXE looks for the ampersand [&] command 
separator, conditional commands (|| or &&), white space (spaces, tabs, and 
commas), percent signs [%] which indicate variables to be expanded, and 
redirection and piping characters (>, <, or |). 
Normally, these special characters cannot be passed to a command as part 
of an argument.  However, you can include any of the special characters in 
an argument by enclosing the entire argument in single back quotes [`] or 
double quotes ["].  Although both back quotes and double quotes will let 
you build arguments that include special characters, they do not work the 
same way. 
No alias or variable expansion is performed on an argument enclosed in 
back quotes.  Redirection symbols inside the back quotes are ignored.  The 
back quotes are removed from the command line before the command is 
executed. 
No alias expansion is performed on expressions enclosed in double quotes. 
 Redirection symbols inside double quotes are ignored. However, variable 
expansion is performed on expressions inside double quotes.  The double 
quotes themselves will be passed to the command as part of the argument. 
For example, suppose you have a batch file CHKNAME.BTM which expects a 
name as its first parameter (%1).  Normally the name is a single word.  If 
you need to pass a two-word name with a space in it to this batch file you 
could use the command: 


        [c:\] chkname `MY NAME`

Inside the batch file, %1 will have the value MY NAME, including the 
space.  The back quotes caused CMD.EXE to pass the string to the batch 
file as a single argument.  The quotes keep characters together and reduce 
the number of arguments in the line. 
For a more complex example, suppose the batch file QUOTES.BAT contains the 
following commands: 


        @echo off
        echo Arg1 = %1
        echo Arg2 = %2
        echo Arg3 = %3

and that the environment variable FORVAR has been defined with this 
command: 


        [c:\] set FORVAR=for

Now, if you enter the command 


        [c:\] quotes `Now is the time %%forvar` all good

the output from QUOTES.BAT will look like this: 


        Arg1 = Now is the time %forvar
        Arg2 = all
        Arg3 = good

But if you enter the command 


        [c:\] quotes "Now is the time %%forvar" all good

the output from QUOTES.BAT will look like this: 


        Arg1 = "Now is the time for"
        Arg2 = all
        Arg3 = good

Notice that in both cases, the quotes keep characters together and reduce 
the number of arguments in the line. 
The following example has 7 command-line arguments, while the examples 
above only have 3: 


        [c:\] quotes Now is the time %%forvar all good

(The double percent signs are needed in each case because the argument is 
parsed twice, once when passed to the batch file and again in the ECHO 
command.) 
When an alias is defined in a batch file or from the command line, its 
argument can be enclosed in back quotes to prevent the expansion of 
replaceable parameters, variables, and multiple commands until the alias 
is invoked.  See ALIAS for details. 
You can disable and re-enable back quotes and double quotes with the 
SETDOS /X command. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs