The best of both worlds: A lightweight, awesome Bash shell on Windows

To be honest, I really like working in Windows 7. It’s fast, it’s stable (for me at least), I love the window arrangement hotkeys, and it’s hardly lacking for software support. Unfortunately, the default command prompt stinks; I feel most comfortable in a UNIX environment, so this article describes how to soup up the bash prompt installed as part of msysgit (Git for Windows). (Let me start by saying that to the best of my knowledge, Windows Power Shell has actually gotten pretty damn capable; several of my coworkers use it to do exactly what I’m going to describe below, and it definitely integrates well into the greater Windows infrastructure. I don’t know, YMMV.)

tl;dr: download Ben Alman’s Bash prompt script to your home folder, then add a line to your .bashrc for your msys shell to source that file.

Screen capture of Git Bash within the Windows context menu

When you install Git for Windows, you get a pretty handy little application called “Git Bash” added to your context menu. This is a lightweight bash shell, provided as part of the MSys build environment. It’s not cygwin, but if you’re like me that’s just the point: I don’t need 99% of what cygwin provides, and Git’s Bash prompt loads quickly, runs fast, and integrates seamlessly with the Windows file structure: $ cd /c/Users/KAdam/Desktop takes me right to my desktop, without any of cygwin’s directory aliasing weirdness. I get ls, less, find, curl, ssh and more, and it even has a basic version of vim. Most usefully, however, MSys Bash lets me use some basic dotfiles.

In the UNIX world, dotfile refers to any file whose filename starts with a period (the “dot”). These are hidden files on UNIX-based systems, and are frequently used for configuration or utility files. For example, a .bashrc file in your user directory (/c/Users/yourAccount/ on Windows) will let you specify custom command-line aliases, preferences and utilities for your user account’s Bash shell without impacting any global settings. Any developer likes to customize their environment, and dotfiles give you tremendous freedom to configure applications to your personal preferences. You can get pretty tricky with these.

So, now we have a lightweight bash shell, and a .bashrc to start customizing it. Where to begin? Searching github for “dotfiles” also turns up over twelve thousand repositories; you need to know what you’re looking for. Personally, I wanted to customize my default command prompt to be a little more useful. By default Bash tells us where we are, and not much more:

Starting screen of MSys Bash, with default command prompt
There’s so much more to life than this.

Ben Alman (@cowboy) is a certifiably awesome guy, and among his many, many github repositories is a very handy set of dotfiles. In particular, I’m a big fan of his custom command prompt; Ben thinks it’s pretty awesome, and I’ve come to fully agree. Check the prompt out on github if you don’t believe me, he walks through what it can do. Most of the dotfiles repo won’t work on Windows, but we’re about to extract and install this great command prompt on our clunky lightweight MSys Bash shell, and it’s going to be great.

Let’s do this all through the command line; learn by doing and all that. From Cowboy’s github page, you can grab the link to the address of the raw command prompt shell script (or just right click and copy that link). We’re going to use cURL to download this file, so fire up your git bash, type cd with no arguments to go to your user directory, and repeat after me:

mkdir ~/.dotfiles
mkdir ~/.dotfiles/source
curl -o ~/.dotfiles/source/command_prompt.sh https://raw.github.com/cowboy/dotfiles/master/source/50_prompt.sh

This creates two directories, one called .dotfiles and a sub-directory called source, and downloads (curl -o) the remote shell script into the newly-created source directory. Where you put this file or what you name it doesn’t really matter, I just like following Cowboy’s convention of storing files within a .dotfiles directory because it seems clean to me.

Now that we’ve downloaded the required shell script, let’s edit our Bash shell configuration file to reference it. If you don’t have a .bashrc in your home user directory, create one from the Bash shell by typing touch .bashrc. We can use the source command to run our downloaded script whenever we open Git Bash. Add this line to your .bashrc:

# Load cool command prompt
source ~/.dotfiles/source/command_prompt.sh

Save the file, then type source ~/.bashrc in your command prompt to re-load the file.

Reload the .bashrc file to activate the custom command prompt style

Now you have a useful, cool-looking Bash prompt, right within Windows. This makes me more productive; hopefully it may help some of you as well.

One thought on “The best of both worlds: A lightweight, awesome Bash shell on Windows

  1. Robert Shook says:

    Great article. I didn\’t know there was such a great and lightweight shell for windows out there. I downloaded this and instantly loved it. Im a programmer and it had all of the smaller utilities i use on a daily basis. Love it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.