[HowTo] Change color of a Linux terminal prompt

In this post, I just will explain how to easily change the color of the text in a Linux terminal. As linux user, I use very often the terminal and I think that default look and feel of terminal is not pretty good. In Linux, Unix and Mac OSX environnement, the prompt settings are stored into a shell variable called PS1. Some other variables like PS2, PS3 and PS4 exist. Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Note : For this post, I use s simple Kali Linux.

So, each distribution has is own settings. For example, in Kali Linux, the content of the PS1 variable is bigger than a Ubuntu or Debian.

root@kali:~# echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

There is a lot of settings but the default terminal on Kali linux is special. The next picture shows this terminal.

kaliTerminal

By default, a standard terminal is set to [\u@\h \W]\$ that mean the following :

  •  \u : display the username of the current session
  • \h : display the hostname (computer name)
  • \W : print the base of the current directory
  • \$ : display # if root, $ otherwise

Now, to modify the current prompt settings, we need to export the PS1 variable. The next command shows you an example.

linux_shell_modified

In this example, there are 2 news escaped characters:

  • \H : display the FQDN hostname
  • \@ : display the current time in format 12 hours AM / PM

Now, lets try to change the color of the terminal. To do this, we need to use the following export syntaxe : \e[x;ym $PS1 \e[m‘ (without the quotes). Where, 

  • \e[ : start the color scheme
  • \e[m : stop the color scheme
  • x;y : The color pair to use
  • $PS1 : The shell prompt variable (or valid values)
[root@kali newbiecontest]$ export PS1="\e[0;32m[\u@\h \W]\$ \e[m "

Here is the result :

linux_terminal_green_prompt

You can try to play with the color pair but the official list is here :

  • Black : 0;30
  • Blue : 0;34
  • Green : 0;32
  • Cyan : 0;36
  • Red : 0;31
  • Purple : 0;35
  • Brown : 0;33

You can see that only the second element change. If you modify the first, which is  ever set to 0 in this example, you can set it to 1 to have the light version of the color.

Please note that this modification is not permanent. If you want to save it, you must write this value in .bashrc file in your home directory ($HOME/.bashrc). Simply append export PS1=”\e[0;32m[\u@\h \W]\$ \e[m ” to the file.

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.