windows: Escape double quotes in parameter

windows: Escape double quotes in parameter

Asked on October 26, 2018 in Windows.
Add Comment


  • 6 Answer(s)

       It is difficult to quickly reproduce the symptoms: if I try myscript ‘”test“‘ with a batch file myscript.bat containing just @echo.%1 or even @echo.%~1, I get all quotes: ‘”test“‘

       Perhaps it is better to try the escape character ^ like this: myscript ‘^”test^“‘?

    Answered on October 26, 2018.
    Add Comment

    Use multiple double-quotes  to escape quotes.For the purpose of making other people’s code legible

    Here’s a set of basic rules:

    •     When not wrapped in double-quoted groups, spaces separate parameters:program param1 param2 param 3 will pass four parameters to program.exe: param1, param2, param, and 3.
    •      A double-quoted group ignores spaces as value separators when passing parameters to programs: program one two “three and more” will pass three parameters to program.exe: one, two, and three and more.

    Now to explain some of the confusion:

          Double-quoted groups that appear directly adjacent to text not wrapped with double-quotes join into one parameter:
    hello”to the entire”world acts as one parameter: hello to the entire world.

     

    Answered on October 26, 2018.
    Add Comment

    Try this code:

    myscript """test"""
    
    

    “” escape to a single “ in the parameter

    Answered on October 26, 2018.
    Add Comment

    The \ sign makes the interpreter interpret the next sign as a character instead of an identifier.

    You see it a lot in code as well:

    "Hello \"World\""
    

    this is interpreted as

    Hello "World"
    

    in your example, in order to pass the arguments to cmd, it needs to be enclosed in “”. But since the arguments to cmd contains ” (and this would end the enclosure) they are appended by \. If the “” would not have been there, the /k dir \"%userprofile%\" would have been interpreted as arguments to runas, not to cmd.

    The reason why they are enclosing the %userprofile% is because this is an environmental variable and will be replaced by text which could contain spaces, which (for the same reason as above) would make the argument to cmd incorrect.

    Answered on January 19, 2019.
    Add Comment

    Under most circumstances, PowerShell can recognize the type of a value by looking at its contents and position in a command line. For example, PowerShell is able to deduce that hello is a string in the following command:

    PS C:\Users\Administrator> write-output -inputobject hello
    hello
    

    Unfortunately, if the string passed to the write-output command contains more than one word separated by a space, PowerShell interprets any word after the first one as additional arguments to the command. Since only one argument is associated with the -inputobject parameter, this causes PowerShell some consternation:

    PS C:\Users\Administrator> write-output -inputobject hello everyone
    Write-Output : A parameter cannot be found that matches parameter name 'everyone'.
    At line:1 char:13
    + write-output  <<<< -inputobject hello everyone
    

    This problem can be overcome by enclosing the string in double quotes (“) as follows:

    PS C:\Users\Administrator> write-output -inputobject "hello everyone"
    hello everyone
    

    This solves the problem of confusing PowerShell. A particular advantage (or disadvantage depending on your point of view) of using double quotes is that variables referenced in the double quoted string are expanded to their actual value. For example:

    PS C:\Users\Administrator> write-output -inputobject "My favorite color is $mycolor"
    My favorite color is red
    

    This can, of course, be a problem if the string is to contain a dollar ($) sign since PowerShell will attempt to interpret this as a variable. For example:

    PS C:\Users\Administrator> write-output -inputobject "Your total is $10"
    Your total is
    

    Another problem involves the display of a string which contains double quotes.Clearly, PowerShell will have difficulty identifying where the string begins and ends:

    PS C:\Users\Administrator> write-output -inputobject "My favorite color is "blue""
    Write-Output : A parameter cannot be found that matches parameter name 'blue'.
    At line:1 char:13
    + write-output  <<<< -inputobject "My favorite color is "blue""
    Answered on January 19, 2019.
    Add Comment

    I cannot quickly reproduce the symptoms: if I try myscript '"test"'with a batch file myscript.bat containing just @echo.%1 or even @echo.%~1, I get all quotes: '"test"'

    Perhaps you can try the escape character ^ like this: myscript '^"test^"'?

    Answered on February 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.