3

cmd can be grouped with /c between the same pair of quotes.

C:\>"cmd /c" echo 1
1

C:\>"cmd.exe /c" echo 1
The system cannot find the path specified.

C:\>"cmd /c"echo 1
 1

C:\>cmd /c"echo 1
1


C:\>c"md /c"echo 1"
 1

I noticed that cmd.exe cannot be quoted in that way, and if there is no space between the closing quotation mark and the command, a leading space is supplied.

How are the quotes parsed? Some basic information that might help: How does the Windows Command Interpreter (CMD.EXE) parse scripts?


Update:

When I copy another program which prints its command-line arguments to the working directory and rename it to cmd.exe, I found they are calling two different executables.

C:\>cmd /c echo 1
...calling my program...
arg0 is <cmd>
arg1 is </c>
arg2 is <echo>
arg3 is <1>

C:\>"cmd /c" echo 1
1

And the leading space is related to the number of quotes.

C:\>"cmd /c"echo 1
 1

C:\>"cmd /c"e"cho 1
1

C:\>"cm"d /c"echo 1
1

C:\>"cm"d /c"ech"o 1
1

C:\>"c"m"d /c"echo 1
 1

C:\>"c"m"d /c"ech"o 1
1

C:\>"c"m"d" /c"echo 1
1

C:\>"c"m"d" /c"ec"ho 1
1

C:\>"c"m"d" "/c"echo 1
1

C:\>"c"m"d" "/"c"echo 1
Microsoft Windows [Version 10.0.19042.685]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\>exit
Tuff Contender
  • 259
  • 4
  • 11
  • I've tried `"C:\WINDOWS\System32\cmd.exe /c" echo 1` and it gives `The directory name is invalid.` – phuclv Sep 20 '21 at 09:22
  • @phuclv, it's likely because the `/` in `/c` is interpreted as path separator, leading to a directory name `cmd.exe ` with a trailing space, which is not allowed… – aschipfl Sep 20 '21 at 09:25
  • Besides the fact that `"cmd /c"echo 1` seems to work, it's also quite interesting that it returns a space in front of the `1`… – aschipfl Sep 20 '21 at 09:32

0 Answers0