bash on Windows

My rose for the day [so far].

On Windows you can use the command explorer SomeDirectory and Windows Explorer will open with the view set to SomeDirectory, assuming one exists as a child of the current working directory. The same holds if you want to jump around the tree a bit. explorer \foo opens foo in the root of the current volume and explorer D:\foo would open D:\foo even if you were currently in the C:\ volume.

Which, once I learned about the awesomeness of Console2 [1], I’ve always created an alias mapping ‘exp’ to ‘explorer’. Because, you know, saving five whole keystrokes seems like a major win. Yes, I’m lazy.

I’ve since switched from the default cmd.exe as my Windows CLI to the bash shell that comes for free from Git [2]. All kinds of awesomeness ensue. On shortcoming however is that *nix paths are separated by ‘/’ while DOS paths are separated by ‘\’. Also, the Git bash refers to volumes using the ‘/c/’ notation while DOS uses ‘C:\’ notation. All of which means I still have to remember which frickin’ shell I’m using when on a Windows box. At least if I want to use the CLI to open Windows Explorer to a specific location, which I often do.

I finally sat down and spent a few minutes fixing my bash ‘exp’ alias a bit.

exp() {
# Grab the dir argument and put it in a friendly container
theDir=$1
# Only drop the volume if it exists otherwise /c* is ambiguous
if [[ "$theDir" == /c/* ]]; then
theDir=${theDir#/c}
fi
# Normalize '/' to '\' for Windows
theDir=${theDir//\//\\}
# Call explorer.exe with the swotted argument
/c/Windows/system32/explorer $theDir
}

Now I can use bash to launch Windows Explorer with an arbitrary directory without having to remember to swap out my path separators. It also means I can use tab-complete instead of having to type the entire path when jumping around the filesystem tree.

Yay!

[1] http://www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspx
[2] http://git-scm.com/downloads

7 thoughts on “bash on Windows

  1. Caveats: Does not do Windows paths well as bash eats the ” treating it as an escape. Also, if you have multiple volumes you’d want to use a more abstract regex in the volume snipping block.

Comments are closed.