How to Change Bash Shell Prompt Colorful and Attractive in Linux

How to Change Bash Shell Prompt Colorful and Attractive in Linux

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.
 
Enjoyed this video?
How to Change Bash Shell Prompt Colorful and Attractive in Linux
"No Thanks. Please Close This Box!"