Is it possible to set up Cygwin so that if I call Cygwin.bat from an arbitrary path using arbitrary Windows-native launching method (cmd.exe, Total Commander' command line), it will start bash prepared in that directory?

For example, if I run cmd.exe and do following (provided that Cygwin.bat is on my PATH):

C:\Users\Me>cd testdir

C:\Users\Me\testdir>dir /b


Cygwin window opens and I'm just where I left off:

me@here /cygdrive/c/Users/Me/testdir
$ ls
myfile1*  myfile2*

me@here /cygdrive/c/Users/Me/testdir

As you can see, when I get Cygwin prompt, I'm already at testdir, not at /home/me.

(More typical scenario where I'm going to use this is running Cygwin from Total Commander's command line, but I guess it should work the same.)

How to achieve this?

  • 'This' is what? How do you want to run Cygwin exactly? – Maximus Aug 17 '12 at 20:04

I was able to hack something together for you. Put this in a batch file in your path (I called mine cygwin.bat, and called it by entering cygwin.bat):

@echo off
set pth=%CD:~2,99%
set pth=%pth:\=/%
set drv=%CD:~0,1%
set "fullpath=/cygdrive/%drv%%pth%"
C:\cygwin\bin\bash.exe --login -i -c "cd ""%fullpath%""; exec /bin/bash"
  • Works like charm, thanks! :) Wasn't able to make a version for "Cygwin Terminal" (using mintty.exe), but since I did not specify which terminal emulator to use, I may keep it for a separate Q. – Alois Mahdal Aug 27 '12 at 9:57

This simpler .bat file (.cmd in my case) is working fine for me:

c:\CygWin\bin\bash.exe --login -i -c "export CD='%CD%'; cd \"$CD\"; exec /bin/bash"

Tested working on:

  • CMD.
  • Total Commander.
  • Remote SSH session.
  • Start Menu --> Run (in this case it opens at C:\Users\Me).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.