DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Eric has posted 2 posts at DZone. View Full User Profile

Starting Cygwin Bash From Windows Explorer

02.22.2010
| 3891 views |
  • submit to reddit
        Want to launch a bash shell in any directory using the Windows Explorer context menu?  This is how I did it, tweaking the basic method described <a href="http://sources.redhat.com/ml/cygwin/2002-05/msg01645.html">here</a>.  This should work even for paths that have spaces or for UNC paths.

1. Edit Registry
- Start regedit.exe.
- Add these key/values:
HKEY_CLASSES_ROOT\Directory\shell\BashHere=bash here
HKEY_CLASSES_ROOT\Directory\shell\BashHere\command=
cmd.exe /c C:\cygwin\cygwin.bat "%1"

Note the quotation marks around "%1".
Please adjust the path to cygwin.bat if necessary.


2. Edit cygwin.bat in c:/cygwin

This batch starts the bash shell with the last command line "bash...".
Insert 
set BASHHERE=%1
 the line before.

This Environment variable now contains the complete path of the opened
folder and is available in the bash shell (echo $BASHHERE).


3. Edit profile 

Make $BASHHERE the current directory by adding the following line to
/etc/profile or ~/.profile. (I'd replace your line cd "${HOME}" with the following):

  if [ "$BASHHERE" != "" ]; then
    cd "$(cygpath -u "$(echo $BASHHERE | tr -d "\042")")"
  else
    cd "${HOME}"
  fi

The "tr -d" deletes enclosing quotes, which cygpath can't deal with for some reason when embedded in $BASHHERE.  Maybe there's a simpler way, but this way worked for me.

(For UNC paths, cmd.exe complains when you launch cygwin.bat, but it dutifully passes on the argument.)