Colore ta console

As for my previous article, I would like to talk about a not well known subject: How to color your console?

As administrator, and in particular when my eyes are tired, I am happy to read « OK » in green and « Not OK » in red. If you google with this subject, you will find many pages with simple tricks, but few with good explanations.

The purpose of this article is not to give hints about colorization, but to explain how it works and the main differences between a Linux terminal and a Windows console.

Linux terminal


A long time ago, a time that the youngest can not know, a computer terminal referred to an hardware device that was used for entering data into (a keyboard), and displaying data (a screen) from a computer. Somehow the end of a network.

VT100 was a video terminal. Its detailed attributes became the de facto standard for terminal emulators.
It communicated with its host system over serial lines using the ASCII character set and control sequences (a.k.a. escape sequences) standardized by ANSI. The VT100 was also the first Digital mass-market terminal to incorporate « graphic renditions » (blinking, bolding, reverse video, and underlining) as well as a selectable 80 or 132 column display.

Today we use mostly virtual terminals (in Linux, the first six virtual terminals provide a text terminal with a login prompt to a Unix shell) or emulators as xTerm that provides VT102 compatible terminals.

Here few escape sequences commonly used by VT100 compatible terminals.

How to colorize my VT100 terminal

The following lines should show « OK » in green and « Not OK » in red.

<br />
echo -e '&amp;#092;&amp;#048;33[32m OK &amp;#092;033[0m'<br />
echo -e '&amp;#092;&amp;#048;33[31m Not OK &amp;#092;033[0m'<br />

The previous script mingles Bash and VT102 escape sequences. Bash interprets and replaces « \033″ by the escape ASCII code. Then the VT102 emulators interpret « <ESC>[32m » as set the foreground color to green.


A good script should work not only on your computer but everywhere. So we should test the capabilities of our environment before relying on it. I found a lot of examples on Internet that perform tests based on $SHELL or $TERM environment variables. From my experience, these are not a good practices as I will explain.

Shell capabilities

$SHELL contains the login Shell, not the current Shell. Therefore scripts that use it are often buggy and people that use several Shells (like me) are very unhappy.

Simple is beautiful! I advice to do a non-portable script or to insert directly the escape character into your script in order to be independent of the Shell (its implementation of the escape characters and on its possible echo builtin command).

Good text editors show special characters, but it is painful to write « <ESC> » on a keyboard (on Windows you can press « ALT » and type « 0027 » on the keypad…)

<br />
echo '&lt;ESC&gt;[32m OK &lt;ESC&gt;[0m'<br />
echo '&lt;ESC&gt;[31m Not OK &lt;ESC&gt;[0m'<br />

Terminal capabilities

A script should check the terminal capabilities before to use VT102 color sequences. We can use the $TERM environment variable and the « test -t fd » conditionnal expression. But it is not easy.

$TERM can take a lot of different values (aterm, xterm, cygwin, etc.) Therefore it is difficult to create an exhaustive list of all terminals that understand the color sequences. Elsewhere, if you are bound to a pseudo-terminal it is not always possible to know the real capabilities of the real end-terminal. And I already experienced few bugs so…

Finally, I think the best practice is to implement a color parameter as the ls command:

  • never means the script will not generate VT102 color sequences
  • always means the script will generate VT102 color sequences
  • auto is where you try to implement a smart default behavior

<br />
function print_help() {<br />
	cat &lt;&lt;EOF<br />
# ...<br />
       --color[=WHEN]<br />
              control  whether color is used to distinguish file types.  WHEN<br />
              may be 'never', 'always', or 'auto'<br />
# ...<br />
EOF<br />
<p>if [ $color = always -o $color = auto -a -t 1 ]<br />
then<br />
	function print_KO() { echo '&amp;#092;&amp;#048;33[31mKO&amp;#092;&amp;#048;33[0m'; }<br />
	function print_OK() { echo '&amp;#092;&amp;#048;33[32mOK&amp;#092;&amp;#048;33[0m'; }<br />
else<br />
	function print_KO() { echo 'KO'; }<br />
	function print_OK() { echo 'OK'; }<br />
fi<br />

Windows console

History (source Wikipedia)

MS-DOS 1.0 did not support the ANSI or any other escape sequences. Only a few control characters (CR, LF, BS) were interpreted.

MS-DOS 2.0 introduced the ability to add a device driver for the ANSI escape sequences – the de facto standard being ANSI.SYS. This continued to work through Windows 98. Extreme slowness and the fact that it was not installed by default made usage by software almost non-existent.

Console windows in Windows versions based on NT (Windows NT 4.0, Windows 2000, Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008) do not support ANSI escape sequences at all. Softwares can manipulate the console with the ioctl-like system Console API.

Few examples

Many script languages (Perl, Tk, and so on) provide library based on the Console API to change the color of a Windows console. If you use them, the commands to change colors is interlaced with the text output.

Another solution is to use a library that provides an ANSI support. So POSIX guys can keep their habbits.

So how you would color your Windows console depends on the programming langage you chose and its helper libraries.


MS-DOS is a very poor language (not only for color). We can change the color of the entire Windows console, but we cannot change the color of a particular line.

<br />
REM MS-DOS example<br />
REM Creates a Black background with light bright green text<br />
COLOR 0A<br />

My advice, do not use MS-DOS to colorize your output or do not use MS-DOS at all if you can.


I’m not completely a fan of PowerShell because of some strange syntaxes, supposed to make life simpler. But it is a very powerful language and, obviously, we can change color output easily.

Here is a powershell function that I use in my daily work. It is able to print a string with ANSI escape sequences to control colors. I use it basically when I run a remote command on a Linux machine.

<br />
### Author: Cyril Martin (mcoolive)</p>
<p>function Write-ColoredHost<br />
{<br />
	Param(<br />
		[switch]$noNewLine,<br />
		[String]$separator=&quot; &quot;,<br />
		[System.ConsoleColor]$foregroundColor = $HOST.UI.RawUI.ForegroundColor,<br />
		[System.ConsoleColor]$backgroundColor = $HOST.UI.RawUI.BackgroundColor<br />
	)<br />
	BEGIN {<br />
		$fgc, $bgc = $foregroundColor, $backgroundColor<br />
	}<br />
	PROCESS {<br />
		$token, $coloredTokens = ([Regex]&quot;&amp;#092;&amp;#048;33[&quot;).split($_)<br />
		Write-Host $token `<br />
				   -noNewLine -separator $separator `<br />
				   -foregroundColor $fgc `<br />
				   -backgroundColor $bgc<br />
		foreach ($coloredToken in $coloredTokens) {<br />
			$colorCodes, $token = ([Regex]&quot;m&quot;).split($coloredToken, 2)<br />
				([Regex]&quot;;&quot;).split($colorCodes) | foreach-object {<br />
					switch -regex ($_) {<br />
						&quot;^0{1,2}$&quot; { $fgc, $bgc = $foregroundColor, $backgroundColor }<br />
						&quot;^30$&quot;     { $fgc       = &quot;Black&quot;                            }<br />
						&quot;^31$&quot;     { $fgc       = &quot;Red&quot;                              }<br />
						&quot;^32$&quot;     { $fgc       = &quot;Green&quot;                            }<br />
						&quot;^33$&quot;     { $fgc       = &quot;Yellow&quot;                           }<br />
						&quot;^34$&quot;     { $fgc       = &quot;Blue&quot;                             }<br />
						&quot;^35$&quot;     { $fgc       = &quot;Magenta&quot;                          }<br />
						&quot;^36$&quot;     { $fgc       = &quot;Cyan&quot;                             }<br />
						&quot;^37$&quot;     { $fgc       = &quot;White&quot;                            }<br />
						&quot;^39$&quot;     { $fgc       = $foregroundColor                   }<br />
						&quot;^40$&quot;     {       $bgc =                   &quot;Black&quot;          }<br />
						&quot;^41$&quot;     {       $bgc =                   &quot;Red&quot;            }<br />
						&quot;^42$&quot;     {       $bgc =                   &quot;Green&quot;          }<br />
						&quot;^43$&quot;     {       $bgc =                   &quot;Yellow&quot;         }<br />
						&quot;^44$&quot;     {       $bgc =                   &quot;Blue&quot;           }<br />
						&quot;^45$&quot;     {       $bgc =                   &quot;Magenta&quot;        }<br />
						&quot;^46$&quot;     {       $bgc =                   &quot;Cyan&quot;           }<br />
						&quot;^47$&quot;     {       $bgc =                   &quot;White&quot;          }<br />
						&quot;^49$&quot;     {       $bgc =                   $backgroundColor }<br />
					}<br />
				}<br />
			Write-Host $token `<br />
					   -noNewLine -separator $separator `<br />
					   -foregroundColor $fgc `<br />
					   -backgroundColor $bgc<br />
		}<br />
		if (!$noNewLine) { Write-Host }<br />
	}<br />
<p>### A little test<br />
$plink= # path of plink (see PuTTY)<br />
$linuxServer= # hostname<br />
$login= # username<br />
&amp;&quot;$plink&quot; -ssh $login@$linuxServer ls --color=always / | Write-ColoredHost</p>

Java : quizz #4

Il y a quelques temps, une de mes connaissances (qui a délaissé le Java au profit du C embarqué) m’a montré quelques lignes de son code en Java et m’a demandé que va-t-il se passer à l’exécution ?

(Ci-dessous le code, que j’ai fortement allégé et gardé le nécessaire pour notre quizz :))

<br />
public class Quizz4 {</p>
<p>	public void handle(Object obj) {<br />
		System.out.println(&quot;Handle object : &quot;+obj);<br />
<p>	public void handle(String string) {<br />
		System.out.println(&quot;Handle string : &quot;+ string);<br />
<p>	public static void main(String[] args) {</p>
<p>		Quizz4 quizz4 = new Quizz4();<br />
		Object obj = null;<br />
		quizz4.handle(null);<br />
		quizz4.handle(obj);<br />
		obj = new String (&quot;my string&quot;);<br />
		quizz4.handle(obj);<br />
		quizz4.handle(obj.toString());<br />
<p>}<br />

Bien entendu, ici je simule certains traitements par un simple d’affichage à la console. Et donc, questions pour vous : à votre avis, si on exécute le code (la méthode main) :

A. le code ne se compile pas.
B. le code s’exécute correctement. Dans ce cas, que verrait on affiché à la console ? Et pourquoi ?
C. le code s’exécute correctement, mais on ne peut pas prédire son comportement. Et pourquoi ?

21 juin 11 – Soirée GigaSpaces au NoSQL User Group

GigaSpaces XAP ! Ce nom vous dit quelque chose ? Certainement car c’est ce que vous allez apprendre si vous êtes inscrit(e) à la session de formation publique GigaSpaces XAP.

Mais si vous n’êtes pas (encore) inscrit(e) à la formation et vous voulez savoir de quoi il s’agit ? Je vous invite alors à venir à la soirée GigaSpaces au NoSQL User Group qui sera assurée par Gauvain, le directeur de notre département Conseil et Solutions.

Ci-dessous les coordonnées de l’événement :

Lieu : OCTO Technology, 50 Avenue des champs Elysees, 75008 PARIS
Date : 21 juin 2011 – 19h
Pas d’inscription  nécessaire

Venez nombreux !