Starting Cygwin in arbitrary directory?

5

1

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
myfile1
myfile2

C:\Users\Me\testdir>Cygwin

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?

Alois Mahdal

Posted 2012-08-17T17:41:28.617

Reputation: 2 014

'This' is what? How do you want to run Cygwin exactly? – Maximus – 2012-08-17T20:04:16.603

Answers

4

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"

ZenoArrow

Posted 2012-08-17T17:41:28.617

Reputation: 66

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 – 2012-08-27T09:57:08.137

1

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).

Sopalajo de Arrierez

Posted 2012-08-17T17:41:28.617

Reputation: 5 328