In test.cmd -
wsl echo "Incoming File - $(printf %%q "$(wslpath -ua "%~dpnx1")")"
wsl ls -la "$(printf %%q "$(wslpath -ua "%~dpnx1")")"
pause
In C:\test, I have two files nospace.txt and has space.txt
When I drag nospace.txt to test.cmd -
C:\test>wsl echo "Incoming File - $(printf %q "$(wslpath -ua "C:\test\nospace.txt")")"
Incoming File - /mnt/c/test/nospace.txt
C:\test>wsl ls -la "$(printf %q "$(wslpath -ua "C:\test\nospace.txt")")"
-rwxrwxrwx 1 yvon yvon 0 Feb 2 18:09 /mnt/c/test/nospace.txt
C:\test>pause
Press any key to continue . . .
When I drag has space.txt to test.cmd -
C:\test>wsl echo "Incoming File - $(printf %q "$(wslpath -ua "C:\test\has space.txt")")"
Incoming File - /mnt/c/test/has\ space.txt
C:\test>wsl ls -la "$(printf %q "$(wslpath -ua "C:\test\has space.txt")")"
ls: cannot access '/mnt/c/test/has\ space.txt': No such file or directory
C:\test>pause
Press any key to continue . . .
It seems WSL does not appreciate escapes for white spaces. Why?
printf %qis only relevant ifwslstarts a shell, for which escaping prepares the content. If it merely passes through a literal argument vector, there's no shell unless any code explicitly starts one, thus nothing to read and reverse that escaping. (Even then,printf %qis only safe when there not only is a shell, but when that shell is guaranteed to be the same shell; bash's printf %q can, with some inputs, generate outputs that cannot be correctly parsed by a baseline-standard/bin/sh).int main(int argc, char**argv)entrypoint isn't the real one but is instead a shim provided by the standard C library, such that a program can replace it with its own; the real entrypoint gets an unparsed command line and can parse it however it chooses. Thus,wslcould be implemented to bypass the default Windows parser and pass the literal text of the command line straight to a UNIX shell if it so chose. (Whereas on UNIX, that's the real interface; programs only see a list of already-parsed C strings).