Bash in Windows

One of the things I like least about programming in Windows is the command prompt.  I don’t know if it’s navigation is just bad or just too different from the Linux terminals that I am used to but I can’t stand using it.  The PowerShell is definitely better, but I would rather just have a unified terminal across my systems not to mentions some of the useful command line applications that are standard with Linux.  And Bash is the winner for me.

I have been using Cygwin on my Windows machines for quite a while now but mostly only for Linux command line programs (ls, ls, ls,…) and it wasn’t until recently that I thought about running a bash terminal through Cygwin.  When you first install Cygwin you will probably get a shortcut on your desktop or start menu for the “Cygwin Terminal” (if not then see Creating a Cygwin Terminal Shortcut) and if you open it you’ll get something like this:


When I first saw this I immediately dismissed it because it was, well, ugly.  However, if you type the help command into this terminal, you will see the following message:


This terminal is running Bash!  Problem solved, end of post right?  Well it’s still not quite the way I want it for a couple of reasons:

  1. By default, the terminal will start in the ‘C:\cygwin64\home\user’ directory (or wherever you installed Cygwin) or it might be the ‘/cygdrive/c/WINDOWS/system32’ directory
  2. It’s still ugly!

Thankfully, both of these can be fixed easily.  I’ll start with the second problem.

Problem 1 – Starting Directory

Problem #1 is annoying for me because I want my terminal to act like Bash terminal but run over my regular Windows files.  Therefore, I don’t want to have to migrate all of my files from ‘C:\Users\user’ to the Cygwin directories; I just want it to open in my Windows user directory.  So, if you’re like me and you want to do this, just follow these steps:

    1. Open the Cygwin terminal as described above.  Make sure it is the Cygwin terminal and not the Windows command prompt.
    2. Navigate to the ‘/home’ directory.
    3. You may or may not have a user directory in the home directory.  If you do, move all of the ‘.*’ files to your Windows user directory and then delete the user directory in ‘/home’.
    4. Create a symlink to your Windows user directory in ‘/home’ by executing the following command: ln -s /cygdrive/c/Users/user where ‘user’ is your Windows username.
    5. If step #3 above did not apply to you, create a .bashrc file in your Windows user directory and edit it.  If it did apply to you, then you can just edit the existing .bashrc file.
      1. Add the line cd /home/user where user is the same username you used in step #4.

Now whenever you open the Cygwin terminal it will place you in your Windows user directory.

Problem 2 – Bash with Style

So the default Bash style through Cygwin is pretty ugly.  This style is controlled through the .bashrc file that you should be able to find in your Linux home directory.  The default Cygwin home directories also come with a .bashrc file but it is mostly empty.  If you already do some custom .bashrc modifications to your Linux machines then you can go ahead and copy them to your Windows machine and you’re all set.  If not, then I would recommend copying the default .bashrc file from your favorite Linux distribution.  If you don’t have access to a Linux machine, then you can usually find a download for these files, such as for Ubuntu.

Once you have a .bashrc file with the styling that you like, you just need to place it in your home directory.  If you followed the directions for Problem #1 above then this will be your Windows user directory.  If not then it will be your Cygwin user directory which is located in the ‘C:\cygwin64\home’ directory (or wherever you installed Cygwin).

Now all of your terminals will have the style of your choosing.


Creating a Cygwin Terminal Shortcut

Cygwin uses a terminal emulator called MinTTY.  You can find this program in the bin folder of your Cygwin installation, which by default is located at ‘C:\cygwin64\bin’.  So all you have to do is create a shortcut to mintty.exe.