batch-filecygwinconsole2cygpath

Open Cygwin on a path passed as the first parameter to bash.exe


I'm going mad trying to achieve this. I read a lot of questions over Stack Overflow, but they didn't work. I'm trying to open a tab on console2 that opens Cygwin on the path passed as a parameter to it.

For cmd.exe, is quite easy:

cmd.exe %1

For Cygwin, this looks really hard:

bash --login -i -c 'cd `cygpath \'D:\Program Files\'`;exec bash'

The problem here is that with path without spaces it works well, with spaces, it doesn't work. Also, I don't know how to pass a param to it, maybe $1 or %1?

Edit 1:

I'm almost there, I created this batch file that should be run instead of bash.exe directly:

@echo off

set CHERE_INVOKES=%CD%
set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
echo %TORUN%
call %TORUN%
PAUSE

This works with all paths except C: (and D:), the reason? Windows is stupid, and instead of having a path called C:, it has a path called C:!!! So, while all paths ends without a backslash, the first path ends with it, driving me mad!


Solution

  • Here is the solution:

    @echo off
    
    set CHERE_INVOKES=%CD%
    ::Remove trailing slash if required
    IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
    
    set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
    
    call %TORUN%
    

    I added this code from this question: Remove Trailing Slash From Batch File Input

    ::Remove trailing slash if required
    IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
    

    In this way I can use this batch file to open Console2 Cygwin on the current path.