Quotes in MSDOS Batch Files

I recently needed to adapt an existing MSDOS batch file to use a cheeky PowerShell command to email a list of recipients. At the time, I though I needed to use quotes (I was wrong - you don’t) but nonetheless, I learnt a little about how MSDOS sets variables in batch files.

In short, it basically doesn’t do any interpretation at all and neither does it need to escape quote characters. Here are some test commands and below, you can see what happens when we run this as a batch file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SET TEST1=THIS IS NICE
ECHO %TEST1%

SET TEST2="JELLO"
ECHO %TEST2%

SET TEST3="JELLO BABY \\""
ECHO %TEST3%

SET TEST3="MY WORD","HOW DO I SURVIVE?"
ECHO %TEST3%

SET "TEST4="THIS","THAT","SOME OTHER""
ECHO %TEST4%

Output:

PS F:\> ./TEST.BAT
F:\>REM SET TEST1=THIS IS NICE
F:\>SET TEST1=THIS IS NICE
F:\>ECHO THIS IS NICE
THIS IS NICE

F:\>REM SET TEST2="JELLO"
F:\>SET TEST2="JELLO"
F:\>ECHO "JELLO"
"JELLO"

F:\>REM SET TEST3="JELLO BABY \\""
F:\>SET TEST3="JELLO BABY \\""
F:\>ECHO "JELLO BABY \\""
"JELLO BABY \\""

F:\>REM SET TEST3="MY WORD","HOW DO I SURVIVE?"
F:\>SET TEST3="MY WORD","HOW DO I SURVIVE?"
F:\>ECHO "MY WORD","HOW DO I SURVIVE?"
"MY WORD","HOW DO I SURVIVE?"

F:\>REM SET "TEST4="THIS","THAT","SOME OTHER""
F:\>SET "TEST4="THIS","THAT","SOME OTHER""
F:\>ECHO "THIS","THAT","SOME OTHER"
"THIS","THAT","SOME OTHER"

I won’t add any huge commentary because it is pretty easy to see exactly what you can expect, but I’d like to draw your attention to the fourth example. Can you see how I even put a quote around the SET command? That works too, but seems redundant. Please file this information away in the “won’t be needing that, thank you“ box :-)


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com