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)
In my last comment I provided a possible solution for
cmd /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:
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 /ccan 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:
The same command will now also work from within bash:
Because the argument
@contains no spaces, WSL won’t add quotation marks and so cmd is “happy”.tl;dr
General way to call
cmd /cfrom bash:/c@after/cExample:
cmd.exe /c @ "Z:\path with space\args.exe" "arg 1" "arg 2"PS: Why would anyone want to use
cmd /cto just call another executable?I start with explaining what’s going on:
There are 4 translation/quoting steps involved in this scenario:
/bin/bashtranslates 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)
(used rules: AFAIK not documented, but it now correctly seems to be inverse to these rules)
Quote from Doc:
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.args.exetranslates 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:
cmd /cin between, then step 2. and step 4. cancel each other out and themainfunction 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:
This works, because the first character, that cmd sees after
/cis not a quotation mark, because the first argument, that WSL sees after/cdoes 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.:/csetPATH=Z:\bin;&args.exefoo barafter step 2.:
/c set PATH=Z:\bin; & args.exe "foo bar"because the first char after
/cis 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:
With such a tool it is possible to call programs like this:
Example output:
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:
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:
And this also works:
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.