Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I see that the caret is the documented escape character.

But, I have an example showing that for the double quote character, ^ doesn't work and you have to use \

C:\>runas /user:Administrator "cmd /k dir \"%userprofile%\""

Why is that, and where is it documented?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

One of the examples in RUNAS /? shows that syntax. The caret is the escape character for CMD.EXE but in Windows individual programs are free to implement their own escape characters and globbing.

share|improve this answer
    
interesting.. and btw.. what is cmd.exe doing with the unescaped quotes at the end? does it keep them, if so why.. Or does it remove them.. if so, why were they needed in the first place? –  barlop Aug 30 '10 at 10:22
    
@barlop: See Michael's answer. –  Dennis Williamson Aug 30 '10 at 14:45
    
are you sure \" is being interpreted by runas and not by cmd.exe? compiling this w.c pastebin.com/28Q2Wxxr to w.exe Compare a)w "a a" b)w \"a a\" c)w ^"a a^" Notice what happens with b in particular. I guess that's the cmd shell treating \ as an escape character. –  barlop Sep 13 '11 at 7:13
    
while it hasn't been cmd.exe that interpreted it, it's not the program either, it's the runtime that split args. i'm not sure if one would call that the program being free to implement it. more like the way the compiler implemented it, or the way the compiler implements it for the programs. any programs written in i suppose, ms visual c, would parse argsv "thusly" msdn.microsoft.com/en-us/library/a1y7w461.aspx –  barlop Aug 28 '13 at 18:07

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.

share|improve this answer
    
Looks to me like RunAs "needs" quotes when that second parameter, the program parameter, has a space. Otherwise it thinks it is being given more parameters than it is. In theory, runas could have been written(I mean coded) to take it to the end, as one parameter no quotes necessary. In contrast to this. cmd.exe e.g. cmd /c dir a b That takes -dir a b- as one parameter. –  barlop Aug 30 '10 at 23:46
    
Also, a more complex point, it looks to me like runas could've done without \" But can't because it is taking the " as start and the next " as end. If it just took the outter-most ones as beginning and end, then it wouldn't need middle ones to be \" e.g. even a nasty looking line like this C:\>runas /user:Administrator "runas /user:Administrator \"cmd /k dir \"%userprofile%\"\" " If it were all " and no \" then theoretically you can still process it. But not so because of the way runas has been written. –  barlop Aug 30 '10 at 23:50
    
@barlop: Don't think of cmd as the argument to runas, because it is not. The argument to runas is cmd /k dir \"%userprofile%\". I don't have the time now, but give me a couple of hours and I should be able to explain a simple C++ program to you and how those programs interpret arguments. I think it would be more clear. So, answer bound to change –  Default Aug 31 '10 at 7:36
    
Actually I may understand and just wasn't clear. I'm aware of the main method and argsv. I heard that they take all arguments in their first element? is that right? Regarding me mentioning cmd.exe, there is an ambiguity whether I mean cmd.exe in that line, or the outter cmd.exe that the line is written in. The only time I mentioned cmd.exe in what I wrote in these comments, was cmd /c dir a b And i'm right that -dir a b- is 1 parameter. But I have been told(don't know if correctly) that all windows programs get all their args as 1 string, presumably argsv[0],then split it themselves –  barlop Aug 31 '10 at 9:22
1  
@Michael, if you want it in Why form.. I could ask. Why is cmd /c dir a b interpreted like cmd /c "dir a b" Whereas with runas, the program parameter(See runas /?) wants quotes. I think runas could've easily been coded to not want them. Since looks to me like theoretically it shouldn't need them. –  barlop Aug 31 '10 at 18:16

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.