Adding color to your shell is going to make your scripts more usable and friendly. You can also pimp or color your BASH prompt, the PS1 environment variable. In this tutorial we are going to show you how we can add color to BASH, both in the shell prompt and in scripts.
So we can proudly echo “Hello world” from our scripts now and in lesson 8 we could see that we can send different echo statements based on interrupts sent to the script. Now we will see how we can make the scripts a little more user-friendly by using colour in our output. and adding color to the PS1 prompt in the BASH shell. Adding color to our scripts will help user focus and adding color to the prompt can make the shell appear just that little more accessible. We do have codes for both foreground and background colors. For example to print in red we could use the following syntax:
echo -e "\e[0;31m Would echo in dark red"
These are the correct codes to use, this does set to dark red, we would also need a rest code to return the color to the shell default, but really! are you interested in these color codes. I don’t think so. It is code like this that makes coding unpopular but it does not have to be difficult. The reality is we can use the codes or we can be a little smarter and use environment variables with the correct codes. The variable names will be the colors we want. Take a look at this file which we could source into our login script or other shell scripts:
export red="\033[1;31m" export green="\033[1;32m" export yellow="\033[1;33m" export blue="\033[1;34m" export purple="\033[1;35m" export cyan="\033[1;36m" export grey="\033[0;37m" export reset="\033[m"
Calling this file /etc/.color as an example, we can source this to our current shell in the following manner. These are environment variables where we have used the export command. This makes the variables available to the current shell and commands launched from the shell. Having created the file once and sharing it in the /etc directory does allow us to access these codes really easily and universally.
$ source /etc/.color
Ideally we could add this to our login script in the same way, either the .bash_profile or the .bashrc file. Using the source command in the same way we can access these variables anytime that want.
To color our bash prompt we could use a PS1 variable similar to this:
$ export PS1="${yellow}\u@\h\w$ $reset"
For a standard prompt with yellow text, we can of course, mix colors if we want.
To pimp not just the BASH shell by adding color we can add color to our scripts bring focus to the elements that we need. This is a simple point of concept script that looks for three files. If they exist the names are in green, if they don’t they are colored red.
#!/bin/bash source /etc/.color for f in file1 file2 file3 ; do if [ -e $f ] ; then echo -e "${green}$f exists!${reset}" else echo -e "${red}$f does not exist!${reset}" fi done
To test the script we can run it where the files do not exist. As we have not put a path to the file names they need to be in the same directory that the script is executed from, where the files do not exist the output is red. Create one or more of these files and you will start seeing green output.
As always, we allow you to see the information in text like this or in a video demonstration. The video is on YouTube and we welcome you to subscribe to TheUrbanPenguin on Youtube.