Changing the color of the command prompt(shell)

December 2016





Purpose


Changing the color of characters in the prompt (or shell), make them more readable. If you are using colors group, you can sense by color, this will eliminate the risk of having a lengthy command prompt.

Information that can be transmitted by the colors

You can assign the green commands for a first machine (host) and blue for another, or green when the system is not loaded, yellow when resources are used or you choose a color for root prompt, another for an MC ...

Methods


Two methods are available are possible. The most used (but the less effective) is to insert escape characters. The following shows the chain commands in blue:

$ PS1="[\033[0;34m][u@h][\033[0m]"


This method has two major drawbacks: its syntax is unreadable, so prone to entry errors, and it is only applicable if the terminal window supports ANSI escape sequence,

Using tput
  • changing color of the prompt(or shell) using tput, your definitions are portable to other terminal emulators.


$ BLUE=$ (tput setaf 4)
$ BLACK=$ (tput setaf 0)
$ PS1=" [$BLUE] u@h]  [$BLACK] "
  • Understanding these lines
  • tput is firstly used to define a variable that contains the sequence of characters to force the color to blue.
  • The second line did the same for black.It is now necessary to know the escape sequences, since tput manages a correspondence file descriptions to all terminals file to consult to find the appropriate sequence terminal physical or emulated.
  • The third line, finally, uses both variable and $ $ BLUE BLACK in the definition of the $ PS1 prompt

variable.
  • Note: You can view a complete list of commands man tput and man terminfo.

Related :

This document entitled « Changing the color of the command prompt(shell) » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.