Weird parsing rule for cmd command #15430

Closed
opened 2026-01-31 04:38:29 +00:00 by claunia · 2 comments
Owner

Originally created by @SainoNamkho on GitHub (Oct 4, 2021).

Windows Terminal version (or Windows build number)

10.0.19042.0, 1.4.3243.0

Other Software

No response

Steps to reproduce

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

note that there's a space in front of 1 in the "cmd /c"echo 1 case

Expected Behavior

When I run it in mintty.exe, command is parsed properly

$ cmd
Microsoft Windows [Version 10.0.19042.685]
(c) 2020 Microsoft Corporation. All rights reserved.

D:\>cmd /c echo 1
cmd /c echo 1
1

D:\>"cmd /c"echo 1
"cmd /c"echo 1
The filename, directory name, or volume label syntax is incorrect.

D:\>"cmd /c" echo 1
"cmd /c" echo 1
The filename, directory name, or volume label syntax is incorrect.

And win+r run dialog box errs in `Windows cannot find 'cmd /c'. Make sure you typed the name correctly, and then try again."

Actual Behavior

It seems this command is parsed specially. When I copied a program named cmd.exe which prints command-line arguments to the current directory, it's not called in this case.

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
Originally created by @SainoNamkho on GitHub (Oct 4, 2021). ### Windows Terminal version (or Windows build number) 10.0.19042.0, 1.4.3243.0 ### Other Software _No response_ ### Steps to reproduce ```cmd 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 ``` note that there's a space in front of 1 in the `"cmd /c"echo 1` case ### Expected Behavior When I run it in mintty.exe, command is parsed properly ```cmd $ cmd Microsoft Windows [Version 10.0.19042.685] (c) 2020 Microsoft Corporation. All rights reserved. D:\>cmd /c echo 1 cmd /c echo 1 1 D:\>"cmd /c"echo 1 "cmd /c"echo 1 The filename, directory name, or volume label syntax is incorrect. D:\>"cmd /c" echo 1 "cmd /c" echo 1 The filename, directory name, or volume label syntax is incorrect. ``` And <kbd>win+r</kbd> run dialog box errs in `Windows cannot find 'cmd /c'. Make sure you typed the name correctly, and then try again." ### Actual Behavior It seems this command is parsed specially. When I copied a program named `cmd.exe` which prints command-line arguments to the current directory, it's not called in this case. ```cmd 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 ```
Author
Owner

@elsaco commented on GitHub (Oct 4, 2021):

@SainoNamkho this is from cmd.exe help, when dealing with invocation using double quote characters:

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

In the 2nd example it succeeds if typed like this:

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

I don't have an explanation for this, but I recall being affected by how "cmd.exe /c" is invoked well before there was a WT.

@elsaco commented on GitHub (Oct 4, 2021): @SainoNamkho this is from cmd.exe help, when dealing with invocation using double quote characters: ``` If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters: 1. If all of the following conditions are met, then quote characters on the command line are preserved: - no /S switch - exactly two quote characters - no special characters between the two quote characters, where special is one of: &<>()@^| - there are one or more whitespace characters between the two quote characters - the string between the two quote characters is the name of an executable file. 2. Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character. ``` In the 2nd example it succeeds if typed like this: ``` C:\>""cmd.exe /c"" echo 1 1 ``` I don't have an explanation for this, but I recall being affected by how "cmd.exe /c" is invoked well before there was a WT.
Author
Owner

@zadjii-msft commented on GitHub (Feb 17, 2022):

"cmd /c" echo 1
"cmd.exe /c" echo 1
"cmd /c"echo 1
cmd /c"echo 1
c"md /c"echo 1"

image

Looks like it's parsed the same in mintty, conhost, and WT. Maybe you had like, clink or something enabled that was affecting the way the commandline was parsed in one of them?

@zadjii-msft commented on GitHub (Feb 17, 2022): ``` "cmd /c" echo 1 "cmd.exe /c" echo 1 "cmd /c"echo 1 cmd /c"echo 1 c"md /c"echo 1" ``` ![image](https://user-images.githubusercontent.com/18356694/154513306-bceebd13-f563-49a6-8a40-71517c826d23.png) Looks like it's parsed the same in mintty, conhost, and WT. Maybe you had like, clink or something enabled that was affecting the way the commandline was parsed in one of them?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15430