Adding Solarized Colors to Bash Scripts

Solarized is one of the most beloved, most supported terminal color themes in the world. It is supported as one of the main terminal color scheme options in the Gnome Terminal without any additional installation. Properly configured a Solarized terminal can save you hours of eye strain and pain. Here’s how to add Solarized colors easily to all your Bash scripts, including your .bashrc for use in your prompt.

Step by Step

Navigate to an existing project repo or create a new one.

Create a new file called solarized.bash to hold your code.

touch solarized.bash

Open the file for editing and add the following.

# source me (don't execute me)


⚠️ That’s a backslash \ (not a forward slash), a semi-colon ; (not a colon), and a lower-case m (not upper).

Save your changes.

Open up a terminal in this directory.

This is just one color to get the idea.

All the strange characters are part of the ANSI terminal escape sequences, which tell your terminal to change colors for everything printed to it from that point on.

Now let’s try to use it by sourcing it from the shell. Remember you can either use . to source or the word source. I tend to use source in scripts and . from the command line because it is quicker.

. solarized.bash 
echo $magenta hello
\033[0;35m hello

Well that doesn’t look right.

This is because the echo command needs to be told about escape sequences that might be in the stuff it is printing out. Add a -e to your echo.

echo -e $magenta hello

You should see hello printed in a color, if you have a Solarized terminal it should be magenta.

Let’s add another color. How about red.

Open your solarized.bash back up and add the following.


⚠️ Don’t forget to save, close, and source the file again to use your changes.

Let’s try both colors.

echo -e $magenta hello $red there
 hello  there

You may have noticed a slight problem here. The colors are working but the spaces are a bit off. That’s because we are including spaces between everything, even if the color codes are invisible and take up no spaces at all. To get rid of the extra space we need to put the colors right next to the words.

echo -e $magentahello $redthere

Doh! Notice that doing that stops everything from working. That’s because Bash is looking for something in $magnetahello instead of $magenta and then hello.

Thankfully Bash has a way to separate the name from the text that follows using curly brackets.

echo -e ${magenta}hello ${red}there
hello there

Much better.

We still have a lot of other Solarized colors to add. But let’s pause to see how we might use them best.

The way we are using them now we always have to use echo -e to get them to show up. But there may be places we would want to use them that do not allow for that. For example, let’s read in a name to greet using the read command.

Type in the following on the shell and then a name when it waits.

read name

Now do the following to print the $name.

echo -e ${magenta}hello ${red}$name

That’s all well and good until we want to prompt the user with some text using the read -p. (Even though we could just use echo or printf this helps demonstrate the problem with echo -e.)

read -p "What's your name? " name

Run that to see the difference.

Now comes the problem I talked about earlier. How do we change the color of the prompt? We can’t put a read -ep because that is not supported.

Here is $() (dollar-parens) come to the rescue.

read -p "$(echo -ne $magenta)What's your name? " name