简体   繁体   中英

Escape spaces in batch file that calls msysgit sh.exe?

I have a run-sh.bat that attempts to pass commands to sh.exe of :

rem My Batch File
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "%1" "

The first parameter is passed in as the command. A simple working example:

run-sh.bat "' echo Hello '"
...
Hello

However, if the command has a path with spaces then the path gets chopped at the first space:

run-sh.bat "' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"
...
C:\Windows\Path\That

Now, if I just paste the entire command directly in the (instead of using the parameter):

rem My Batch File
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "%1" "
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c ""' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"" "

Then you can see that for that second echo the path comes out right:

run-sh.bat "' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"
...
C:\Windows\Path\That
C:\Windows\Path\That Contains\Some Spaces\In\It

How can I get it to work as a parameter?

Should I modify the ? Or change the way I am formatting the parameter at command line?

Tested solutions:

Using CMD:

  rem My Batch File
  CMD /c ""%programfiles(x86)%\Git\bin\sh.exe" --login -i -c '%*'"

You don´t have to use CMD /C though. You can run it directly to avoid extra quotes:

  rem My Batch File
  "%programfiles(x86)%\Git\bin\sh.exe" --login -i -c '%*'

Then when you call it, only use quotes on the path:

run-sh.bat echo "C:\Windows\Path\That Contains\Some Spaces\In\It"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM