cygwin, mingw, ConEmu and start up directory

One of the most asked questions about cygwin usage: ‘How to run its shell in the specified working directory?’

The problem

Finally, thanks to csbdess, Issue 1552: can’t set working dir for cygwin consoles from commandline, the problem was identified.

Cygwin always do cd "${HOME}" from /etc/profile.

# Make sure we start in home unless invoked by CHERE
if [ ! -z "${CHERE_INVOKING}" ]; then
  unset CHERE_INVOKING
else
  cd "${HOME}" || echo "WARNING: Failed attempt to cd into ${HOME}!"
fi

So, if you do not like that behaviour, read below for solutions.

NB. MinGW (at least version installed on my PC) does not have checking for CHERE_INVOKING environment variable. There is just cd "$HOME" at the end of /MinGW/msys/1.0/etc/profile. You may change that file yourself to get the same checks as cygwin.

CHERE

Cygwin will not do cd "${HOME}" if environment variable CHERE_INVOKING is defined. So, if you run your shell (bash) as following, shell will be started in proper working directory.

NB ConEmu’s default tasks created for cygwin already has set CHERE_INVOKING=1 defined.

set CHERE_INVOKING=1 & c:\cygwin\bin\sh.exe --login -i

Thought, you can, but must not change the contents of your /etc/profile commenting cd "${HOME}"… Because you can change cygwin behaviour within ConEmu tasks or command line.

ConEmuWorkDir

Another workaround - do cd to the working folder in your shell profile (.bashrc, .zshrc, etc.)

if [ -n "${ConEmuWorkDir}" ]; then
  cd "$ConEmuWorkDir"
fi

ConEmu set ConEmuWorkDir to the console startup directory, so you may use it in your scripts. Read more about ConEmuEnvironment in docs.

git-cmd –no-cd

If you are using Git for Windows then you don’t have to use cygwin solutions.

The bootstrapper git-cmd.exe has switch --no-cd which allows to inherit startup directory. The default task {Git Bash} created by ConEmu utilize the switch. Don’t forget to refresh default tasks.

cygdrive

Not related to working directory and ConEmu, actually, but let it be here.

If you want to remove annoying /cygdrive, just replace one line in your /etc/fstab.

#none /cygdrive cygdrive binary,posix=0,user 0 0
none / cygdrive binary 0 0
Download    Donate