WSL: problem passing arguments with spaces to a process running under cmd.exe

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.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (2 by maintainers)

Most upvoted comments

In my last comment I provided a possible solution for cmd /c "set PATH=Z:\bin; & args.exe "foo bar"":

User@PC:~$ cmd.exe /c set "PATH=Z:\bin;" "&" "args.exe" "foo bar"

It works, because the first token of the commandline (set) doesn’t contain spaces.

If the first token does contain spaces, there is is a simple workaround: Example on Windows without workaround:

C:\Windows>cmd /c ""Z:\path with space\args.exe" "arg 1" "arg 2""

Command-line arguments:
  argv[0]   Z:\path with space\args.exe
  argv[1]   arg 1
  argv[2]   arg 2

From within WSL you can’t produce such a strange commandline, with unescaped quotes within a quoted argument - because except for cmd there is no windows executable, that is using such a strange syntax.

Luckily, cmd /c can be persuaded to not remove quotation marks from the commandline, such that a much more “normally” looking commandline (without unescaped quotes within a quoted argument) similar to this can be used:
cmd /c "Z:\path with space\args.exe" "arg 1" "arg 2"
This exact commandline won’t work, because cmd will remove the first and last quotation mark. To “persuade” cmd to not remove ", we can simply add a single not-" char that doesn’t affect the rest of the command as the first argument after /c: The letter @.
Example on Windows with workaround:

C:\Windows>cmd /c @ "Z:\path with space\args.exe" "arg 1" "arg 2"

Command-line arguments:
  argv[0]   Z:\path with space\args.exe
  argv[1]   arg 1
  argv[2]   arg 2

The same command will now also work from within bash:

User@PC:/mnt/c/Windows$ cmd.exe /c @ "Z:\path with space\args.exe" "arg 1" "arg 2"

Command-line arguments:
  argv[0]   Z:\path with space\args.exe
  argv[1]   arg 1
  argv[2]   arg 2

Because the argument @ contains no spaces, WSL won’t add quotation marks and so cmd is “happy”.


tl;dr

General way to call cmd /c from bash:

  • don’t enquote the complete cmd-command after /c
  • add the single-letter-argument @ after /c

Example: cmd.exe /c @ "Z:\path with space\args.exe" "arg 1" "arg 2"


PS: Why would anyone want to use cmd /c to just call another executable?

I start with explaining what’s going on:

There are 4 translation/quoting steps involved in this scenario:

  1. /bin/bash translates the given command into the executable name and an array of arguments.
    (because on *nix new processes are started with an argument array)
    (used rules: https://www.gnu.org/software/bash/manual/html_node/Quoting.html)
  2. WSL/lxss translates the array of arguments into a single string. (because on Windows, new processes are started with a single argument string)
    (used rules: AFAIK not documented, but it now correctly seems to be inverse to these rules)
  3. cmd.exe (a very old program…) does a weird string transformation, that breaks all usual behavior:
    Quote from Doc:

Processing quotation marks

If you specify /c or /k, cmd processes the remainder of String, and quotation marks are preserved only if all of the following conditions are met:

  • You do not use /s.
  • You use exactly one set of quotation marks.
  • You do not use any special characters within the quotation marks (for example: & < > ( ) @ ^ | ).
  • You use one or more white-space characters within the quotation marks.
  • The String within quotation marks is the name of an executable file.

If the previous conditions are not met, String is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.

The German help-text is actually a little bit more clear: The phrase “closing quotation mark” is not the next quotation mark, but the last quotation mark of the string.

  1. args.exe translates the single argument string into an argument array.
    (because the C-Standard needs an argument array) (Rules depend on the compiler used to compile args.exe, however almost all compilers use these rules)

Some notes:

  • It seems to be step 2. that changed with the Fall Creators Update.
  • I think the new behavior is much better (as I already told here), because if you don’t call some cmd /c in between, then step 2. and step 4. cancel each other out and the main function of the new process gets exactly the arguments, that it should get. This was previously not always the case if " or \ were contained in some arguments.

Currently possible solution for your last example:

User@PC:~$ cmd.exe /c set "PATH=Z:\bin;" "&" "args.exe" "foo bar"

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

This works, because the first character, that cmd sees after /c is not a quotation mark, because the first argument, that WSL sees after /c does not contain a space.

Translation steps (I represent arrays as bullet-lists):
Original string:
/c set "PATH=Z:\bin;" "&" "args.exe" "foo bar" after step 1.:

  • /c
  • set
  • PATH=Z:\bin;
  • &
  • args.exe
  • foo bar

after step 2.:
/c set PATH=Z:\bin; & args.exe "foo bar"
because the first char after /c is not " cmd doesn’t remove any quotes and it works.


This is of course not a nice solution - but there is no really nice solution for windows executable that don’t follow typical rules.

I’d hope that Microsoft provides some wrapper, that just takes one argument, and uses that as complete commandline for Windows, for example:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   if (argc!=2){
      printf("\nPlease call with exactly one argument.\n");
   }else{
      system(argv[1]);
   }
}

With such a tool it is possible to call programs like this:

/path/to/call.exe ‘commandline exactly as you would type in cmd or Win+R-Dialog’

Example output:

User@PC:~$ /mnt/d/call.exe 'cmd /c "z:\bin\args.exe "foo bar""'

Command-line arguments:
  argv[0]   z:\bin\args.exe
  argv[1]   foo bar
User@PC:~$ /mnt/d/call.exe 'cmd /c "set PATH=Z:\bin; & args.exe "foo bar""'

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

Hi, After reading this with interest, I have been digging further and found a “clean” solution. I leave it here for future reference.

In a batch file, if you ECHO %CMDCmdLine%, you get something that gives you the expected synthax:

C:\WINDOWS\system32\cmd.exe /c "“C:\Folders with spaces\TESTS.bat” “PARAM_TEST_STRING1” “PARAM_TEST_STRING2"” Note the double double-quotes after /C and at the end. This way, you can pass your double-quoted params.

If you run such a command from VBS, for instance, you also have to provide double quotes, which becomes a forest a double quotes since to get a double-quote inside a string (double-quotes limited) you have to double it, eg.:

Set objShellApplication = CreateObject(“Shell.Application”) strArgs = " ““PARAM_TEST_STRING1"” ““PARAM_TEST_STRING2"” " objShellApplication.ShellExecute “C:\WINDOWS\system32\CMD.exe”, “/C “””“C:\Folders with spaces\TESTS.bat”” " & strArgs & " “””, “”, “”, 1

Have a nice day ! Have a nice day, Pierre.

Thanks, @TSlivede. To answer your question: you will want to use ‘cmd /c’ when you want to run more than one command in the same environment. In my case I ran into this issue because I wanted to call “dumpbin.exe” from the WSL command line. I had an alias that launched a cmd.exe process, called VCVARS32.BAT, and then called dumpbin.exe with the command line parameters I passed. It broke with the Fall Creator’s Update.

Format like this “"” + variable + “"”

This works:

tt1='dir'
tt2='C:\Program Files (x86)'
cmd.exe /c @ "$tt1" "$tt2"

And this also works:

export tt='dir "C:\Program Files (x86)"'
WSLENV="$WSLENV":tt cmd.exe /c @ %tt%

I don’t think the issue is resolved yet. At the minimum we need documentation on how WSL converts an array of arguments from bash into the string that is passed to the Windows process.

The second example (cmd /c "z:\bin\args.exe \"foo bar\""), which you analyzed, works the same in a Windows shell as a Bash shell. It is the first example (cmd.exe /c "z:\bin\args.exe "foo bar"") that is problematic. It generates different (and undesirable) behavior in Bash. My last example (cmd /c "echo %TMP% && ver") works fine in Bash because there are no arguments with embedded spaces. I used it only to demonstrate that the string passed to cmd.exe must be enclosed in quotes.

The problem remains that I can’t send an argument with embedded spaces to a Windows process. Below is yet another example that hopefully fully demonstrates the problem. It works in the Windows shell but not the Bash shell. I can’t figure out a way to make it work in Bash. If you can then please share.

root@DESKTOP-MVKOR9S:~# cmd /c "set PATH=Z:\bin; & args.exe "foo bar""

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