Developers Club geek daily blog

2 years, 11 months ago
Multi-colored terminals

In this publication I will tell about some tricks which will decorate everyday life of any Linux system administrator (and not only). All of them are connected with bash cover PS1 variable. The PS1 variable defines how the invitation for input of new commands will look. And each user can redefine it as will wish, for example, in the file ~ / .bashrc (which is executed at start of bash and is used for including for a configuration).

For a start we will consider simple option, my favourite format of the command line.

PS1='\t j\j \u@\h:\w\n\$ '

The result will be here such:
17:42:46 j0 olleg@petrel:~

This normal use of the PS1 variable, but if I did not begin with it is the story would be incomplete. Usually in the PS1 variable define an invitation format for command input by special strings. The detailed list of these sequences can be read in documentation to bash, in this example:

  • \t — an output of "the current time", actually it turns out time of completion of the previous command, it is convenient when before eyes.
  • j\j — displays the character of j and after it quantity of the started job, i.e. processes in a background. Too it is convenient to have it before eyes that a case about them not to forget when you are going to close the terminal.
  • \u@\h — a user name and the name of the server. If you work with several servers via remote terminals — not to be confused.
  • \w — after a colon — a working directory.
  • \n — as the line turned out though informative (something it seems the status of bar), but long, we invite to enter commands from a new line, and this top line will visually separate from result of work of the previous command.
  • \$ — on a new line will be the character or $ for the normal user or # for root'a is displayed and having separated it a space it is possible to invite to enter new command.

It would seem what else to wish … But further it will be more interesting. The matter is that by means of special control characters it is possible to set color of the output text, color of the cursor and even to redefine title bar at such graphic terminals as Gnome2. And, in my opinion, it is quite convenient when in the color the terminals started on different servers separate. For me each server is associated with some color and this color we will paint the command line and the cursor on each server.

My .bashrc is separated into two files, .bashrc contains the general code for all servers, and in .bash_local — settings of the command line, unique for this server. I will insert .bash_local into .bashrc the special directive. Let's begin with .bash_local. In the context of this article there I will have two lines which define color of this server:

# .bash_local
# change cursor and prompt color

Just I bring codes of color in variables. But, as you noticed that a method of a task of color for the cursor and for the text of the command line — different. For some reason so historical it turned out. To understand what color by what code is coded, is the suitable picture.


In the middle — designation of color for color of the cursor, from below — designation of color for the text. As you can see that I for the text and the cursor use color of a sea wave. Since the name of the petrel server ("petrel"), it is associated at me with this color.

Now .bashrc, I show it not completely too but only what is related to a subject:

# .bashrc
# local stuff
[[ -f ~/.bash_local ]] &&. ~/.bash_local

Here I insert a code from .bash_local into the general file. Thus to decide earlier described variables on color of the server.

# set to red

I define two more variables with purely red color, it will be used for marking of terminals of the priveligirovanny user (root'a).

#my favorite PS1
case "$TERM" in
   PS1='\[\e[38;5;'$prompt_color'm\]\t j\j \u@\h:\w\n'
   [[ $UID == 0 ]] &&{ prompt_color=$root_prompt_color;cursor_color=$root_cursor_color; }
   PS1="$PS1"'\[\e[m\e]12;'$cursor_color'\a\e[38;5;'$prompt_color'm\]\$ \[\e[m\]'
   PS1='\t j\j \u@\h:\w\n\$ '

Here it is checked what the terminal is used. For any unknown or not supporting color the invitation without color (PS1=' \\u@\h:\w\n\$ t j\j') will be used as I described it at the beginning of article. But if the name of the terminal begins on xterm or rxvt, for example, so itself positioned by the Gnome terminal, begin to kudesit with a color. The first line — we set text color — color of the server and we display the first line of the invitation of command input. It will be always colored in color of the server. The second line — we check whether we work under the nepriveligirovanny or priveligirovanny user (root'om). If root — that we redefine colors on red. The third line — we create the second line of the invitation and we define color of the cursor in the terminal. I.e. there at us it will turn out or $ and through a space the cursor, both servers painted color if the user normal. Or red # and through a space the red cursor if it is root.

# If this is an xterm set the title to user@host:dir
case "$TERM" in
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

And it if it is fair, one in one is copied from initial .bashrc from Debian. I know that this code alters title bar at a window, places information on the user, the server and a house directory there. But as this code was thought up not by me, will not comment on it.

As a result at us has to it will turn out as on the picture at the very beginning of the publication.

This article is a translation of the original post at
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here:

We believe that the knowledge, which is available at the most popular Russian IT blog, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus