In Unix I could run myscript '"test"'
and I would get "test"
.
In Windows cmd
I get 'test'
.
How can I pass double-quotes as a parameter? I would like to know how to do this manually from a cmd
window so I don't have to write a program to test my program.
Best Answer
Another way to escape quotes (though probably not preferable), which I've found used in certain places is to use multiple double-quotes. For the purpose of making other people's code legible, I'll explain.
Here's a set of basic rules:
program param1 param2 param 3
will pass four parameters toprogram.exe
:param1
,param2
,param
, and3
.program one two "three and more"
will pass three parameters toprogram.exe
:one
,two
, andthree and more
.hello"to the entire"world
acts as one parameter:helloto the entireworld
."Tim says, ""Hi!"""
will act as one parameter:Tim says, "Hi!"
Thus there are three different types of double-quotes: quotes that open, quotes that close, and quotes that act as plain-text.
Here's the breakdown of that last confusing line:
Thus, the text effectively joins four groups of characters (one with nothing, however):
Tim says,
is the first, wrapped to escape the spaces"Hi!
is the second, not wrapped (there are no spaces)is the third, a double-quote group wrapping nothing
"
is the fourth, the unwrapped close quote.As you can see, the double-quote group wrapping nothing is still necessary since, without it, the following double-quote would open up a double-quoted group instead of acting as plain-text.
From this, it should be recognizable that therefore, inside and outside quotes, three double-quotes act as a plain-text unescaped double-quote:
will print
Tim said to him, "What's been happening lately?"
as expected. Therefore, three quotes can always be reliably used as an escape.However, in understanding it, you may note that the four quotes at the end can be reduced to a mere two since it technically is adding another unnecessary empty double-quoted group.
Here are a few examples to close it off:
Final note: I did not read any of this from any tutorial - I came up with all of it by experimenting. Therefore, my explanation may not be true internally. Nonetheless all the examples above evaluate as given, thus validating (but not proving) my theory.
I tested this on Windows 7, 64bit using only *.exe calls with parameter passing (not *.bat, but I would suppose it works the same).