Before we get started with the fun part, let's get some of the basics right! In addition to the capability to do many important things like moving cursor positions, printing new lines, etc, Escape sequences form the core of printing colourful text on a terminal prompt. There's a wide range of Escape characters defined for the Linux terminals -- for example, \n for new line, \b for backspace, etc.
To get started, we need the Octal Escape sequence \()33 for printing colourful words. After encountering the Escape character, the console looks for the instruction and acts inunediately, based on the instruction and its parameter(s).
Although there are a set of instructions, and each instruction can have many different parameters, we will focus On CSI (Control Sequence Introducer) instructions. CSI (represented by '[') looks for parameters or a group of parameters. The parameters are normally a set of decimal letters. When we have a group of parameters, it should be separated with a semi-colon.
The action of the CSI sequence is regulated by the end character. For our purposes, the end character will be m Oower-case M), which is responsible for the character display attributes. So that's enough with the theory part; let's now get started with some practical examples of putting colour on that dull terminal.
Note: The article has been written taking the xterm terminal and Bash shell into consideration.
Changing Jhe foreground colour
Try the following arguments with the 8cIw command: This will print "Linux, the great!" in green. Let us decrypt the arguments of the echo statement: -e will enable interpretation of the backslash-escaped characters.
• /033 is the code for the Escape character; encountering this, the console moves to escape mode.
• / is the instruction to the above Escape sequence to switch to the Command Sequence Introducer (CSI) mode.
Now CSI will look for a set of cligital characters. As I have mentioned earlier, we can give multiple parameters separated by commas. 1 is the first parameter after CSI, which tells the console to print the letters in bold format. The following are a few other options:
• 0 - reset to default
• I-bold
• 2 - half bright
• 4 - underscore
• 5- blink
If we don't give the above parameter, the printing will be in the default format.
Next is the main part-printing the characters in colour. Table 1 shows the decimal codes that regulate the foreground colour of the characters. As we have given the number 32, the characters will be printed in green.
m at the end sets the character attributes as per the above mentioned codes.
Finally, I used codes again at the end so as to restore the default colour (which is white in my case!).
Changing the background colour
This is as easy as foreground colour change; simply substitute the colour codes from 30-37 to 40-47 and you are ready to go. Table 2 shows the code for background colours.
Colouring the bash. prompt
The main bash prompt is stored in the PSI variable. The following are the special characters that are decoded to their respective meanings in the typical Fedora prompt variable:
• \u = Usemame;
• \h = Hostname;
• \w = Present Working Dir;
To simplify matters, let us store codes in variables:
• RED="\[\033[1;31m\]"
• CYAN="\[\033[1;36m\]"
• WHITE="\[\033[1;37m\]"
Finally, the PSI will look like what follows:
PSl="[${CYAN}\u${RED)@${CYAN)\H:${RED)\w${WHITE)] "
Colouring the Is output
Many of us are aware of the is -color, to print the file listing in the colour. After understanding colour codes we can customise the listing colour as per our choice.
dircolors is the command that is used to set the LS_ COLOR variable, which in turn regulates the colour output of the is command. Type dircolors -p to print the default colour listing.
Here you will see variables like FILE, DIR, EXEC, etc, with some colour codes attached to them. Also, there are entries to specify separate colour codes for files with particular extensions. In order to customise things, we need to modify the respective entries. First, store the colour database in one file as follows:
Now, open the a. colors file and modify the entries as you prefer. For example, to add the listing of Perl files in blue, you will put: .pl 01;34. And to have underlined output of the executable, I will modify EXEC to EXEC 04;31 After doing the changes, save the file. Now type: .. and you are ready to go:
In order to make these colour settings permanent, put the above command in your login script. Just like LS_COLOR, we can also have output of grep in colour. Type: That's about it; we are done with the basic knowledge of a colourful world. So, go ahead and put some colour on your terminal.




Reply With Quote
Bookmarks