Git Bash Is My Preferred Windows Shell

Page contents

About shells

A shell is a layer or interface between you and an operating system. There are a lot of command-line shells for Windows, including:

Over the years, I’ve used all six of the above Windows shells. I recently installed Git for Windows and now I can add another shell to the list of Windows shells that I’ve used:

  • Git Bash

About bash

Bash is a an acronym for “Bourne again shell” and it is a command-line shell that was created for Unix-like systems. Details are at wikipedia.org/wiki/Bash_(Unix_shell).

What is Git Bash

Git Bash is a Windows command-line shell that is packaged with Git for Windows. It emulates the bash shell that is familiar to nix-nux users.

💡
Git Bash is useful even if you do not use git.

 

Why I Prefer Git Bash

I like Git Bash because…

  • It’s easy to install.

  • It has a small footprint.

  • It installs a Git Bash Here command that is available when right clicking on a folder in Windows File Explorer.

  • I can pipe the output of commands through the less pager, which is better than the more pager.

  • In addition to less, many other bash commands are available including cat,⁠[1] clear, curl, cut, date, grep and friends,⁠[2] gunzip, gzip, head, history, jobs, mkdir, printenv, ps, pwd, scp, sed, sftp, ssh, tail, tar, touch, wc, which, and zcat.

  • I can view built-in help for a command with, for example, less --help.

  • Almost any command-line Windows app that’s on my path, for example hugo.exe and scoop.cmd, can be launched either within a Git Bash script or at a Git Bash command prompt (possibly with the help of winpty).[3]

  • I can write simple bash scripts…

    • and not have to worry about WSL file system issues[4] because Git Bash uses the regular Windows file system,

    • and all my Windows environment variables are available.

  • It has active and helpful discussion groups, for example superuser.com/questions/tagged/git-bash and stackoverflow.com/questions/tagged/git-bash.

 

Launching Git Bash

To launch Git Bash…

If you right click on the default Desktop Git Bash shortcut and choose Properties, you will see something like the following.

Target: "C:\Program Files\Git\git-bash.exe" --cd-to-home

Start in: %HOMEDRIVE%%HOMEPATH%

You can change these shortcut properties if you like.

💡
During the installation of Git for Windows, you can specify the install directory. For example, I changed the default, which is C:\Program Files\Git\, to C:\Program Files\GitForWindows\ because this helps me remember the full name of the app in this directory.

 

Git Bash customizations

~/.bash_profile

If ~/.bash_profile exists, the commands in it are run every time you start a Git Bash session. To create and edit this file do the following.

  1. Launch Git Bash.

  2. At the Git Bash command-line prompt, run one of these commands:

    nano ~/.bash_profile
    vim ~/.bash_profile

    Note that the text editors nano and vim are included in Git Bash and are on your PATH. (If you do not know how to use vim, I recommend that you use nano, otherwise it may take you a while to figure out how to exit vim.😆)

  3. Add whatever commands you’d like to this file. Here is an excerpt of my .bash_profile:

    ## Created: 2020-11-28
    
    ## use `gvim filename` to edit filename with GUI Vim
    alias gvim="/C/Windows/gvim.bat"
💡
After you change your .bash_profile, I recommend that you close all Git Bash sessions and all Windows File Explorer windows, and then relaunch Git Bash and make sure your new .bash_profile settings worked.

 

MinTTY color scheme🌈

The default Git Bash terminal emulator is MinTTY. I prefer the solarized color scheme so I edited ~/.minttyrc so it includes the following.

ForegroundColour=101,123,131
BackgroundColour=253,246,227
CursorColour=220,50,47

Black=7,54,66
BoldBlack=0,43,54
Red=220,50,47
BoldRed=203,75,22
Green=133,153,0
BoldGreen=88,110,117
Yellow=181,137,0
BoldYellow=101,123,131
Blue=38,139,210
BoldBlue=131,148,150
Magenta=211,54,130
BoldMagenta=108,113,196
Cyan=42,161,152
BoldCyan=147,161,161
White=238,232,213
BoldWhite=253,246,227

This is the contents of the .minttyrc.light file, which is part of github.com/mavnn/mintty-colors-solarized. Thank you Michael Newton (@mavnn) for making this color scheme available on GitHub!

 

MinTTY font and character set

To change the character set and fonts used in the MinTTY terminal editor, right click anywhere in a MinTTY window or title bar, choose Options, choose Text in the left pane, and then…

  1. Click Select… and choose DejaVu Sans Mono, 10pt (or whatever you prefer) for the font.

  2. Under Character Set, choose UTF-8 (or whatever you prefer).

These and other settings will be appended to the bottom of your ~/.minttyrc file.

 

Git Bash tips

Case insensitivity

Normally Unix-like shells are case sensitive, but Git Bash, in general, is case insensitive.

 

Paths

To view your current path, run the following command, which means “print working directory.”

pwd

To go to your Git Bash home directory, which is also known as ~, run either of the following equivalent commands.

cd
cd ~

On most systems, your Git Bash home directory is:

/c/Users/username/

Note that in Git Bash…

  • The path delimiter is slash (/), which is also known as forward slash.

  • C:\ is mounted as /c/

 

Start

When you are at a Git Bash prompt, you can launch a file or directory in your system’s default app by using the Start command, which is equivalent to the start command (thanks to case insensitivity). For example, to open the current directory in Windows File Explorer, use this:

start .
      👆
     Notice this dot (.)

To open an HTML file in your default web browser, use this:

start ReleaseNotes.html

 

Environment variables

To find out the environment variables available to Git Bash, run:

printenv

This includes the PATH environment variable, which lists the directories that are searched for executables.

 

Scripting

I’d rather write a bash shell script than a Windows batch file to deal with apps, files, and folders that live in my Windows file system. Thanks to Git Bash, It’s easy to do this. (I have learned the hard way that it’s not so easy to do this in WSL.⁠[4])

 

Where to put scripts

When you run printenv, you can see that the ~/bin/ directory is on your PATH. This is a reasonable place to put your Git Bash scripts. To create this directory, run this command:

mkdir ~/bin/

 

Example script

One of my scripts is called gvim-winpath and it looks like this:

#!/bin/bash

## Created: 2020-11-28
## Filename: gvim-winpath
## Usage: gvim-winpath "C:\path\to\filename"

/c/windows/gvim.bat `cygpath "$1"`

I use this to launch gvim on a file that is specified using a Windows-style backslash path. To learn about the cygpath command, which is used in this script, run one of the following commands at a Git Bash prompt.

cygpath --help
cygpath --help |less

If you pipe the cygpath --help output to the less pager, you need to know that the way to exit less is by typing either of the following.

q
Q
💡
cygpath is analogous to wslpath, which I use in qutebrowser Tips and Fragments in section 8. Calling a WSL bash script from a Windows userscript🔨.

 

No need to chmod

In most Unix-like shells, you need to chmod +x executables but in Git Bash this is not needed.

 

Case insensitivity exceptions

--help and other command arguments

Arguments to nix-nux commands, in general, are case sensitive. For example, in the following

cygpath --help

The command cygpath is case insensitive and its argument --help is case sensitive. For example, this works:

Cygpath --help

but this does not work:

cygpath --Help

 

exit

To quit Git Bash, you can either click the close-window X in the upper right corner of the terminal window or type the following at the command prompt:

exit

This exit command must be all lower case and does not show up when you run which exit.🤔

 

Please edit this page 👍 👎 📝

Endnotes


1. cat stands for concatenate.😸
2. grep stands for “get regular expression.” Its friends include egrep and fgrep.
3. When launching an app from Git Bash, it’s sometimes useful to append  & to the end of the command so it will be launched as a background job.
4. To learn about some problems with WSL, see Windows Subsystem for Linux: The lost potential by Julio Manuel Merino Vidal. Also see the Hacker News comments about this article at news.ycombinator.com/item?id=25154300.