Search code examples
bashbatch-filemsys2

Batch "%1" and path spaces


On Windows I made a file called mingw64_open_with.cmd and associated .sh bash files so that they open with it, the goal being to be able to double click .sh bash scripts on Windows to run them through MinGW. This is my .cmd script:

@echo off
C:\msys\usr\bin\env.exe MSYSTEM=MINGW64 c:\msys\usr\bin\bash.exe -l -c "cd $(dirname "%1") && "%1""

It runs perfectly as long as the path to the .sh script, which is %1 here, doesn't contain spaces. When it does I get this: c:\msys\usr\bin\bash: line 1: cd: too many arguments. Nothing I tried, such as doubling the " solved the problem, I don't know what to do.


Solution

  • This works for me:

    C:\msys\usr\bin\env.exe MSYSTEM=MINGW64 c:\msys\usr\bin\bash.exe -l -c "cd \"$(dirname \"$1\")\"; \"$1\"" sh %1