I've got lots of experience in a Linux environment, but now I need to do something in Windows. I'm having some trouble figuring out how to correctly escape spaces in a windows command environment. The rules about how it parses the command are completely unclear.
For example, in a Linux environment, I can create the following script:
my command.sh
#!/bin/bash
echo "Called as '$0' with argument '$1'"
and execute it like so, with all possible ways of escaping spaces being equivalent.
[bradworkubuntu:~/command_test] my\ command.sh arg\ with\ spaces
Called as './my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] "my command.sh" arg\ with\ spaces
Called as './my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] my\ command.sh "arg with spaces"
Called as './my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] "my command.sh" "arg with spaces"
Called as './my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] /home/brichardson/command_test/my\ command.sh arg\ with\ spaces
Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] "/home/brichardson/command_test/my command.sh" arg\ with\ spaces
Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces'
[bradworkubuntu:~/command_test] "/home/brichardson/command_test/my command.sh" "arg with spaces"
Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces'
but when I try something similar on Windows, the results are totally inconsistent.
my command.bat
echo "Called as '%0' with argument '%1'"
executed like so:
C:\Users\brichardson\Documents\command_test>my^ command.bat arg^ with^ spaces
'my' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\brichardson\Documents\command_test>"my command.bat" arg^ with^ spaces
C:\Users\brichardson\Documents\command_test>echo "Called as '"my command.bat"' with argument 'arg'"
"Called as '"my command.bat"' with argument 'arg'"
C:\Users\brichardson\Documents\command_test>my^ command.bat "arg with spaces"
'my' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\brichardson\Documents\command_test>"my command.bat" "arg with spaces"
C:\Users\brichardson\Documents\command_test>echo "Called as '"my command.bat"' with argument '"arg w
ith spaces"'"
"Called as '"my command.bat"' with argument '"arg with spaces"'"
C:\Users\brichardson\Documents\command_test>\Users\brichardson\Documents\command_test\my^ command.ba
t arg^ with^ spaces
'\Users\brichardson\Documents\command_test\my' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\brichardson\Documents\command_test>"\Users\brichardson\Documents\command_test\my command.ba
t" arg^ with^ spaces
C:\Users\brichardson\Documents\command_test>echo "Called as '"\Users\brichardson\Documents\command_t
est\my command.bat"' with argument 'arg'"
"Called as '"\Users\brichardson\Documents\command_test\my command.bat"' with argument 'arg'"
C:\Users\brichardson\Documents\command_test>"\Users\brichardson\Documents\command_test\my command.ba
t" "arg with spaces"
C:\Users\brichardson\Documents\command_test>echo "Called as '"\Users\brichardson\Documents\command_t
est\my command.bat"' with argument '"arg with spaces"'"
"Called as '"\Users\brichardson\Documents\command_test\my command.bat"' with argument '"arg with spa
ces"'"
C:\Users\brichardson\Documents\command_test>C:\Users\brichardson\Documents\command_test\my^ command.
bat arg^ with^ spaces
C:\Users\brichardson\Documents\command_test>echo "Called as 'C:\Users\brichardson\Documents\command_
test\my command.bat' with argument 'arg'"
"Called as 'C:\Users\brichardson\Documents\command_test\my command.bat' with argument 'arg'"
C:\Users\brichardson\Documents\command_test>C:\Users\brichardson\Documents\command_test\my^ command.
bat "arg with spaces"
C:\Users\brichardson\Documents\command_test>echo "Called as 'C:\Users\brichardson\Documents\command_
test\my command.bat' with argument '"arg with spaces"'"
"Called as 'C:\Users\brichardson\Documents\command_test\my command.bat' with argument '"arg with spa
ces"'"
C:\Users\brichardson\Documents\command_test>"C:\Users\brichardson\Documents\command_test\my command.
bat" "arg with spaces"
C:\Users\brichardson\Documents\command_test>echo "Called as '"C:\Users\brichardson\Documents\command
_test\my command.bat"' with argument '"arg with spaces"'"
"Called as '"C:\Users\brichardson\Documents\command_test\my command.bat"' with argument '"arg with s
paces"'"
Can anyone explain to me when Windows command prompt will or won't escape spaces? And what it will actually store in the environment when it does or doesn't?
Near as I can tell, it only escapes spaces in the command path, and only if the drive letter is included. And if you use quotes to deal with spaces, it includes them in whatever string gets passed to the command.