Start bash shell (cygwin) with correct path without changing directory

13

3

I need to run a cygwin shell script without changing the working directory, from outside the cygwin environment.

If I run just \path\to\bash.exe -c command, then the path is not set correctly and cygwin programs can't be found.

If I use the --login option, bash sets the path correctly, but also changes to my home directory.

What options should I use to launch bash to keep the current working directory, and also find cygwin executables?

Ben Voigt

Posted 2011-10-12T22:02:36.283

Reputation: 6 052

Hi, similar question was being discussed at StackOverflow: http://stackoverflow.com/questions/9637601/open-cygwin-at-a-specific-folder

– Roman Hocke – 2014-01-16T11:15:12.820

@Roman: This would be totally off-topic on SO. – Ben Voigt – 2014-01-16T12:47:19.550

Answers

18

It turns out that the cygwin default /etc/profile checks for an environment variable named CHERE_INVOKING and inhibits the change of directory if set.

So

set CHERE_INVOKING=1
\path\to\bash.exe -l -c command

has the desired behavior.

Ben Voigt

Posted 2011-10-12T22:02:36.283

Reputation: 6 052

This works for invoking MSYS2 bash as well. – tharen – 2015-01-09T08:32:55.930