Skip to content

problem passing arguments with spaces to a process running under cmd.exe #2835

@raymod2

Description

@raymod2

With the Fall Creators Update I can't figure out a way to pass arguments with spaces to a process running under cmd.exe. Consider the following program compiled to args.exe:

#include <stdio.h>

int main(int argc, char **argv)
{
   int i;

   printf("\nCommand-line arguments:\n");

   for (i = 0; i < argc; i++)
   {
      printf("  argv[%d]   %s\n", i, argv[i]);
   }
}

Here is the output from a Windows shell:

C:\Users\Dan>cmd /c "z:\bin\args.exe "foo bar""

Command-line arguments:
  argv[0]   z:\bin\args.exe
  argv[1]   foo bar

C:\Users\Dan>cmd /c "z:\bin\args.exe \"foo bar\""

Command-line arguments:
  argv[0]   z:\bin\args.exe
  argv[1]   "foo
  argv[2]   bar"

Note that the inner quotes cause foo and bar to be treated as a single argument unless you escape those quotes. Here is from a WSL session:

root@DESKTOP-MVKOR9S:~# cmd.exe /c "z:\bin\args.exe "foo bar""

Command-line arguments:
  argv[0]   z:\bin\args.exe
  argv[1]   foo
  argv[2]   bar
root@DESKTOP-MVKOR9S:~# cmd.exe /c "z:\bin\args.exe \"foo bar\""

Command-line arguments:
  argv[0]   z:\bin\args.exe
  argv[1]   "foo
  argv[2]   bar"

Here foo and bar are treated as separate arguments whether you use quotes or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions