Rainbow Bash Shell: How-To Guide

Don’t you hate it when you get an error message that is so long, you can’t even find where it started? Make your shell rainbow-colored to make your life easier! Here are some fun terminal tricks. By Katie Thomas (Hackbright Academy – fall 2013 class)

I hate when I get an error message that is so long, I can’t even find where it started.

The solution? Rainbow terminal!

I’m going to break down the steps to make your shell look like the one above. (ps: I’m on Mac OSX.)

First, what my shell used to look like –

It’s easy to get lost in all the white text, so let’s change the prompt to be colored to be able to find it easily in a sea of white text.

I found out some other cool terminal settings that I’ll share below. For example, on a daily basis, I don’t care that I’m on “Katies-MacBook-Pro”, so I got rid of that.

Before we dig into the code, where is it? The configurations for your bash shell live in a file called “.bash_profile” in your root directory.

Run the following commands in terminal to find your bash shell:

$ cd ~ # takes you to your root directory
$ ls -a # lists all files in directory, even invisible dot files

If it isn’t there, you’ll need to make one. That’s no big deal. Run:

$ touch .bash_profile

Once you’re in the file, you can start playing!

How to change the look of different types of files:

1 : alias ls = ' ls -F '

First, this line creates an alias for the command ls. What this means is that, when I call ls at the prompt, ls -F is actually being run. The -F option displays different types of files with different markers and colors. For example, directories are shown as blue and followed by a slash.

How to get more specific on ls colors:

2: export CLICOLOR=1
3 : export LSCOLORS=gx

Next, I didn’t like the color of the directories, so I used lines 2 and 3 to change the color from blue to cyan. Line 2 basically says “it’s ok to change colors” and line 3 says “change the foreground color of the directory listing to cyan without changing the background color.” More information can be found here.

What I originally set out to do, and then some:

4 : PS1='[e[0;32m]ue[m] [e[0;34m]w[e[m] [e[0;32m]$[e[m] [e[0;37m]'

Finally, I used this line to change the look of my prompt. This line looks nasty with all of the escapes and brackets, but you can play around with it and see how changing different parts changes the look of your prompt.

I’m going to point out a couple of things. The large bold u stands for user. This is what makes “katiemthom” appear at my prompt. The underlined parts of this line set the color and weight of that part of the prompt. The w stands for working directory, and the $ stands for… $.

If I had wanted “Katies-Macbook-Pro” to appear, like it did in my old prompt, I could have used the variable “h”.

I didn’t want any part of my prompt to appear bolded, so I set the first argument to 0. The 32 corresponds to green, 34 to blue (looks purple on dark background) and 37 to white.

So that’s it! The last thing you do is run:

$ source .bash_profile

…to make the changes appear! (Alternatively you can quit the shell and reopen it.)

Here is a step-by-step guide I found helpful while playing with my .bash_profile.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply