There are many problems for program arguments.
To understand them, you need to know how the line is parsed.
First the line is parsed by cmd.exe.
There are many rules (and phases), but in your case there are only two relevants.
Each quote toggle the quoted-mode
, in the quoted-mode, special characters lose their special
meaning
A caret escapes the next character, the caret itself will be removed.
The caret can also escape a quote, to avoid the activation of the quoted-mode.
This only works in unquoted mode, inside quotes the caret loses its special meaning.
Backslashes have no special meaning for cmd.exe.
For your examples, cmd.exe will parse them to:
printer.exe arg1 \" ^" arg2
-> printer.exe arg1 \" ^" arg2 --- The caret is inside quotes
printer.exe arg1 ^"arg2
-> printer.exe arg1 "arg2 --- The caret was outside quotes
On windows each program.exe
is responsible for splitting the command line into arguments, despite to a linux, where the shell decides how to split the arguments.
The consequence for windows programs is:
A total mess!
You need to know the rules of every program, to know how the line is splitted into arguments.
There are programs that have rules with support of backslashes or double double-quotes, some with single quotes and some without the ability to build arbitrary arguments.
For some more explanations How does the Windows Command Interpreter (CMD.EXE) parse scripts?