Bash Shell Colors!


Today I changed the colors in my bash shell and added the current git branch to the command line. The git branch was actually what I was going for…the colors were a nice bonus I found along the way 😄

Note added Oct 2018 for clarity: I use MacOS which by default uses the bash shell in both the Terminal program and in my text editors’ command line. If you use a different shell or framework like ZSH, or one of the many windows shells, your implementation of the below would be slightly different.

Bash Shell Scripting

I grazed the surface of this a while back, and learned a bit more about shell scripting today! The main thing was learning about the prompt script PS1 which determines what shows on the command line and how it looks. By default, mine was set up like this:

Computer-Host-Name:current-folder username$

I was fine with this, but wanted to also add in the git branch. This YouTube video prompted this whole rabbit hole and led me to a gist which shows how to figure out what git branch you’re on and add it to the command line. But a simple copy paste (without really understanding the script) gave me a longer command prompt:


No good! So then I found this article which did a good job explaining what all the letters and numbers in the script mean. For example:

\u  username
\h computer host name
\w path to current directory
\W current directory (folder name only)

Now we’re getting somewhere! So I edited the script a bit, found some additional color options (or even more if I want to adjust later), and now I’ve got a concise prompt with colors I like. I also created aliases for the colors to make the script a bit easier to read. Here’s the final outcome:

# Colors
Color_Off="\[\033[0m\]" # Text Reset
Blue="\[\033[0;34m\]" # Blue
Cyan="\[\033[0;36m\]" # Cyan
BrightBlue="\[\033[0;94m\]" # Bright Blue

# Git branch function
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

# Format prompt script
export PS1="$BrightPurple\h$Purple:\W$Cyan\$(parse_git_branch)$Color_Off \u$ "

Update 2019: I refined this more than once! Latest post encapsulating all of my updates is here.

Sourcing Changes Without Closing Terminal

One thing to note: rather than restarting Terminal every time you make changes to the .bashrc file where all of these scripts go, you can just re-source it: source ~/.bash_profile

Then all the new colors, shortcuts, or scripts are available in the current bash session. Note here that my .bash_profile file is already pointed to .bashrc.

Terminal Contrast Adjustments

After I got it working in Terminal, I went to Atom and opened the terminal there. The colors were all wrong! Turns out this is because Terminal adjusts colors for better contrast, so (on my screen at least) “blue” looked more purple, but the shell in Atom doesn’t make these adjustments.

Thankfully the terminal package I use (platformio-ide-terminal) lets you change the ANSI colors, so I did this to match the default colors in Terminal. Voila! Great, consistent, perfect-contrast colors no matter which shell I’m using.

Other Stuff

All of this took a good few hours out of my day. So much for working on the actual work I planned to do 😂