Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does single-quoting do in Windows batch files?

I recently ran a command in windows cmd with single quotes in it and found that it did not behave as I expected.

From trying to google "windows batch single quote vs double quote" I've found a few articles where people asked about having windows treat single quotes like double quotes, and about how bash treats single quotes and double quotes differently. However, I couldn't easily find an explicit explanation of what single quotes do in windows batch commands.

So what do single quotes do in windows batch files? Do they perform any sort of quoting functionality at all?

like image 336
math4tots Avatar asked Jun 11 '14 22:06

math4tots


People also ask

What do single quotation marks do?

Single quotation marks are also known as 'quote marks', 'quotes', 'speech marks' or 'inverted commas'. Use them to: show direct speech and the quoted work of other writers. enclose the title of certain works.

What is the difference between a single and double quote?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

What does %% mean in batch files?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is the difference between single quote and double quote in C?

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character 'x' and a null terminator '\0'. So “x” is two-character array in this case.


1 Answers

Single quotes are not used at all by the cmd.exe command processor except to enclose the command to run within a FOR /F statement:

for /f %%A in ('someCommand') do ... 

Or to specify a string to process by FOR /F if the USEBACKQ option is used:

for /f "usebackq" %%A in ('some string') do ... 

All other quoting handled by cmd.exe is done with double quotes. However, there are Windows utilities (external commands) like WMIC that use single quotes for their own quoting purposes. But there is no standard - each utility is free to have its own interpretation of single quotes.

The inconsistent escape and quote rules used by cmd.exe vs. various command line utilities often makes it a challenge to discover the correct syntax needed for any given situation.

Here are some important notes about using double quotes " with cmd.exe.

  • Double quotes are a simple state machine. The first " turns quoting on, the next turns it off.

  • All special characters are treated as literal values when quoted, except for the % and <newLine> characters always, and the ! character if delayed expansion is enabled, and the ^ character if delayed expansion is enabled and ! also appears somewhere within the line.

  • If quoting is off, then you can escape a " as ^" to prevent it from turning quoting on. But once quoting is on, you cannot escape the closing ". The very next " will always turn quoting off.

like image 89
dbenham Avatar answered Oct 12 '22 20:10

dbenham