Windows batches in Cygwin with spaces in path and arguments

938 Views Asked by At

Consider this dummy Windows batch script:

echo %1

Supposed just to echo to the terminal its first argument.
Assume its path in resp. Windows, Cygwin style is:

c:\test\win.bat 
/cygdrive/c/test/win.bat 

From Cygwin bash:

$ c:\test\win.bat  "hello world"
"hello world"

So quotes correctly identify a single argument.

But now let us introduce spaces in path:

"c:\te st\win.bat" 
/cygdrive/c/te\ st/win.bat 

Then:

$ /cygdrive/c/te\ st/win.bat "hello world" 

Gives:

"C:\te" is not recognized as an internal or external command, operable program or batch file.

The same happens with:

$ "/cygdrive/c/te st/win.bat" "hello world" 

It should be noted this:

$ /cygdrive/c/te\ st/win.bat "hello"
hello 

That is hello is now passed to win.bat unquoted (and with "/cygdrive/c/te st/win.bat" "hello" either).

How can I have spaces both in the path and the argument?

1

There are 1 best solutions below

1
On
$ echo "echo %~1" > /cygdrive/c/te\ st/win.bat    
$ cat /cygdrive/c/te\ st/win.bat
echo %~1
$ cmd /c $(echo "c:\te st\win.bat"| sed 's/ /^ /g') "aaa bbb"

C:\Users\Me>echo aaa bbb
aaa bbb