简体   繁体   中英

Backslash and final quote

Question is: why final backslash in argv arguments are interpreted as escape-backslash, not as backslash '\\\\' itself So code:

#include <iostream>
int main(int argc, char **argv)
{
    if (NumArgs > 1)
        std::cout << pArgsArray[1] << std::endl;
    return 0;
}

Program.exe "C:\\Dir\\"

expected output: C:\\Dir

real output C:\\Dir"

so why final backslash is not interpreted as-is like othes backslahes??

same result we obviosly get in C#

The backslash acts as an escape operator when it is applicable. \\D is not an escape code, so bash seems to be passing it through just fine. \\" is an escape code however. In order to achieve the output you desire you should write:

Program.exe "C:\\Dir\\"

Command-line escapes in cmd are downright crazy. Spaces, pipe characters etc. are escaped with a caret: ^ . Quotes are escaped with a backslash: \\ . A backslash which doesn't immediatly precede a quote is taken literally.

I don't know if there's a way to end a quoted argument with a backslash. Maybe doubling the backslash could help. Some sources also suggest ^\\ to escape such a backslash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM