Linux: Sequences Escape-Control to colorize scripts [Quick Guide]
for the digital age, we seek always to view items or to accentuate the Visual components to highlight any particular conclusion. But be aware that on the shell Linux you can also use some visual artifacts to highlight certain elements.
It is often the case in scripts where you want to distinguish the case of a positive or negative test. a well known example of GNU/Linux administrators: it’s the starting or stopping of the system operating. We very often find the steps highlighted by an indicator [ OK ] or [ ERROR ] , as shown in the shot below:
in fact, this is very easily achievable on any script, as long as you follow the next procedure. In fact, it is possible to colorize the text of our scripts, to put it in italics, bold highlighted, with a more intense display…
Use cases are generally related to the fact of wish:
– to highlight a point capital . But we don’t want the user to have control over the indicators. You can then use vim to activate contrast of elements. Because, the tool supports the coloring, highlighted, bold, italic type effects… In this case, there is no guarantee that on another post vim will be him also configured the same way. Portability is not assured.
– have the opportunity to make use of readable and portable solution. must be used “escape” ANSI codes. These guarantee ease of use but also the portability and flexibility.
This mode allows to control the color of the text and the background of display.
WARNING : today, most devices support this display mode. But it may be that some former VT require a setting, or even does not support.
The codification is quite complicated, because it implements a sequence of multiple keys. Thus, for the color of text are available the following controls:
– Black: ESC [30m
– Red: ESC [31m
– Green: ESC [32m
– yellow: ESC [33m
– Blue: ESC [34m
– Magenta: ESC [35m
– Cyan: ESC [36m
– white: ESC [37m
– default: ESC [39m
then you can also control the color of the background of the display]]]]]]]]] , via sequences below:
– Black: ESC [40m
– Red: ESC [41m
– Green: ESC [42m
– yellow: ESC [43m
– Blue: ESC [44m
– Magenta: ESC [45m
– Cyan: ESC [46m
– white: ESC [47m
– default: ESC [49m
you can also add effects to text as the highlighted, bold, italics, using the following sequences]]]]]]]]] :
– FAT: ESC [1m
– pointed out: ESC [4m
– flashing: ESC [5m
– Reset/default: ESC [0m
Finally, it is quite possible to mix these components to give several effects to a text or a wallpaper.]]]]
example: to list red text flashing on a white background, it will write the sequence: ESC [5;31;47m
Note: the ESC symbolism is usually codified, within a script, by “