How to Change Bash Shell Prompt Colorful and Attractive in Linux

March 29, 2017 | By
| 1 Reply More

Most of us work with a shell prompt. By default most, Linux distro displays hostname and current working directory. But only a few may be knowing about customizing the Bash prompt. You will be excited to know that you can change the color of your shell prompt to impress your friends or to make your own life quite easy by adding some important information to the command prompt while working.

In this article, I'll explain how to change the color or customize your Bash Prompt in some easy ways.

Understanding BASH

The bash shell is the default under any Linux distributions. Prompt is control via a special shell variable called PS1. There are other variables too, like PS2, PS3, and PS4. Bash displays the primary prompt PS1 when it's ready to read a command. And it displays the secondary prompt PS2 when it needs more input to complete a command.

In short, bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Display Current Bash Prompt

You can view the current Bash Prompt status by running this command below:

# echo $PS1
[\u@\h \W]\$

By default, the command prompt is set to [\u@\h \W]\$. Each backslash-escaped special characters can be decoded as follows:

  • \u : Display the current username.
  • \h : Display the hostname
  • \W : Print the base of current working directory.
  • \$ : Display # (indicates root user) if the effective UID is 0, otherwise display a $.

For a Unix user other than root, it will be displayed as below:

[linodadmin@centos-01 ~]$

Modifying the Bash prompt

As discussed before, the bash prompt is controlled by a variable named PS1, and we can adjust this variable in your .bashrc file to customize your prompt.

In addition, if you want to make these changes available for all system users on the system or globally, all you need to do is modify this variable in the /etc/bash.bashrc file (on Debian and Ubuntu systems) or /etc/bashrc (on other Linux distributions) instead of ~/.bashrc.

Bash allows you to use some shortcuts to retrieve details, such as the user name, the hostname name, the current working directory, date & time etc. These shortcuts are called escape sequences.

Take an example, you want to display the user’s name, hostname, the current directory and the time in 12-hour format followed by $. Then it can be retrieved by modifying the PS1 variable with these escape sequences which displays the required information as below:

  • \u : Display the current username.
  • \h : Display the hostname
  • \W : Print the base of current working directory.
  • \@ : Display current time in 12-hour am/pm format
$ export PS1="[\\u@\\h \\W \\@]\\$"
[linodadmin@centos-01 ~ 01:50 PM]$

This will allow only a temporary change to your Bash prompt. If you need to make a permanent change to the succeeding terminals, you can edit the ~.bashrc file with this PS1 value (PS1="[\\u@\\h \\W \\@]\\$") towards the end of the file.

Check out some of the lists of escape sequences which will help us to retrieve our required information.

  • \u  Username of the current user,
  • \w The current working directory
  • \W The last fragment of the current working directory. For example, if you are currently in /home/linodadmin/var, this will give you var.
  • \h The name of the computer, upto a dot(.). For example, if your computer is named centos-01.linoxide.com , this gives you centos-01.
  • \H FQDN hostname
  • \d The date in “Weekday Month Date” format (e.g.”Tue 21 March”)
  • \t The current time in 24 hour HH:MM:SS format
  • \T The current time in 12 hour HH:MM:SS format
  • \@ The current time in 12-hour AM/PM format
  • \n Move on to the next line.
  • \! : the history number of this command
  • \# : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \j : the number of jobs currently managed by the shell

Adding Colors to the Prompt

Mostly, system admins would like to add some color to their dull shell prompt. This can be achieved with the help of ANSI escape sequences in the PS1 variable. These escape sequences need to be enclosed in \[ and \] in order to work properly. In a simple way we can use this command syntax to add colors to the shell prompt.

'\e[x;ym $PS1 \e[m'

Where:

  • \e[ : Start color scheme.
  • x;y : Color pair to use (x;y)
  • $PS1 : Your shell prompt variable.
  • \e[m : Stop color scheme.

Check out the list of color codes which can be used:

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
bakgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset

Let's see the examples using these color codes.

Suppose, you want to use different colors in one terminal statement itself like you want to show the username in red and the directory path in cyan, followed by a yellow, bold $ symbol. You need to use escape sequences separately as required.  For example, the escape sequences for red is \e[31m, for cyan, it is \e[36m, and for yellow, it is \e[33m. For bold text, we need to use \e[1m. In addition, we need the reset ANSI escape sequence, which prevents styles from affecting the rest of the text in the shell. The reset sequence is \e[0m. The PS1 variable for this will look like this.

export PS1='\[\e[32m\u\] \[\e[36m\w\] \[\e[33m\]\[\e[1m\]$ \[\e[0m\]'

The export statement should be added to your $HOME/.bashrc file for permanent changes.

Using tput command

We can even use tput command to modify the prompt settings.  For example, to display Yellow color prompt using a tput we can use this command below:

export PS1="\[$(tput setaf 3)\]\u@\h:\w $ \[$(tput sgr0)\]"

List of some of the tput command line options below:

  • tput bold – Bold effect
  • tput rev – Display inverse colors
  • tput sgr0 – Reset everything
  • tput setaf {CODE}– Set foreground color, see color {CODE} table below for more information.
  • tput setab {CODE}– Set background color, see color {CODE} table below for more information.

Color Codes for tput command line options

Check out the examples using these tput commands below:

Modifying Prompt settings using Bashish

Bashish is a theme environment for text terminals. It can change colors, font, transparency and background image on a per-application basis. Furthermore, it supports prompt changing on common shells such as bash, zsh and tcsh. We can install these tool for our Linux distributions from the download link.

I installed it using its tar extract.

root@centos-01:/home/bashish-2.2.4 $ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
configure: creating ./config.status
config.status: creating data/main/prompt/sh/autoload
config.status: creating data/bashish-version
config.status: creating Makefile
config.status: creating bashish.spec
config.status: creating man/bashish.1
config.status: creating bin/bashish

Bashish configuration:

prefix: /usr/local

Now run 'make' and 'make install'
root@centos-01:/home/bashish-2.2.4 $ make
root@centos-01:/home/bashish-2.2.4 $ make install

Now we need to run bashish to install user configuration files.

$bashish
This will enable Bashish prompt and terminal themeing

the following files will be modified:
~/.profile
~/.bashrc
~/.bash_profile (if already exists)
~/.zprofile (if already exists)
~/.zshrc
press ENTER to enable Bashish theming
or hit CTRL+C to quit

Bashish is now installed.

Restart your terminal or logoff and logon again to start theming your shell.

Next you must restart your shell by typing the following command:

$exec bash

┌────[root@centos-01]───────[15:58:35]───────[/home/bashish-2.2.4]─────────────────────────────────────────────────────────────────────────────
└──#

You can type the command bashish list to view all the available custom themes and bashish --help to view more options.

─# bashish list
amiga
amigados
appchat
appcompress
appeditor
appeditornofont
appeditor_reversed
appevilroot
appgeneric
apphearttracker
appinvertfgbg

You can apply the required changes to the command prompt by typing bashish THEMENAME, for example

$bashish moo

This will modify your command prompt as in the snapshot.

Similarly, you can apply any of these custom themes to modify your command prompt or terminal settings.

Adding Smileys on the prompt

Furthermore, you will be glad to know that we can even make smileys to display on our prompt to have expressions corresponding to our work (successful/failure) attempts on prompt. For example, you want to display a green happy smiley on our successful attempts and red sad smiley on our failure attempts. Then we can set a logic script to grab the boolean value to display the smileys corresponding to the returned variable value.

export PS1='$(if [[ $? == 0 ]]; then echo "\[\e[32m\]:)"; else echo "\[\e[31m\]:("; fi)\[\e[0m\] \u \w $ '

The $? variable holds the return value of the previous program. It contains a 0 if it succeeded, and a non-zero value if it failed. The $(if ... fi) block contains the logic for displaying the smileys.

Adding the Emojis on the prompt

You can modify the same logic string with the Unicode characters for the emojis instead of smileys to convey the emotions more effectively. Please see the modified string with the emoji variant of our smiley prompt. :) is represented as \xf0\x9f\x98\x83 and :( is represented as  \xf0\x9f\x99\x81.

export PS1='$(if [[ $? == 0 ]]; then printf "\xf0\x9f\x98\x83"; else printf "\xf0\x9f\x99\x81"; fi)\[\e[0m\] \u \w $ '

However, depending upon the terminal emulator and the font used on our systems, we can see monochrome emojis or even garbled characters.

Conclusion

I hope you've enjoyed reading these few nice techniques for coloring your bash prompt. The visual distinction by color is what we can do in our terminal to make our work more enjoyable and tracking. Colorizing certain information can make them stand out, and can help you locate the last prompt when scrolling through terminal history. You can even customize color patterns to distinguish your root users with others.

Now, it's time for you to utilize this effectively to ease your work. I would love to read your valuable comments and suggestions on this.

Filed Under : HOWTOS, LINUX HOWTO

Tagged With : ,

Free Linux Ebook to Download

Comments (1)

Trackback URL | Comments RSS Feed

  1. Adrien Massy says:

    Finally a very comphrehensive and explicit howto.

    Thank you for the work.

Leave a Reply

All comments are subject to moderation.