Customize your bash prompt and add colors to it

To customize the theme of your bash prompt, you need to modify the PS1 env variable in your bashrc. If you don’t know what the bashrc is; check out this answer at stackexchange.

The PS1 env variable is responsible for displaying the prompt on the terminal emulater that you are using.

Now there are some special characters that you can use to display different values in the bash prompt.

Following are some of the special characters that can be used:

For example if you do

$ export PS1="\u@\h:\w $ " 

you will get the output as Bash-customization-special-characters.png

The complete list of these special characters can be found in the bash documentation.

To display text in different colors on the terminal, you can use tput. For example

$ tput setaf 82

In this command 82 is the code for color green, so you will get the output as Bash-customization-colors.png

Similarly there are color codes for the different colors. To set the terminal color back to whatever it was previously; the command is.

$ tput sgr0

You can use these special characters and tput command to create new bash themes.

Following is a sample configuration.

## Basic prompt colors.
yello=$(tput setaf 11)
grey=$(tput setaf 7)
maroon=$(tput setaf 9)
green=$(tput setaf 82)
bold=$(tput setaf bold)
reset_p=$(tput sgr0)

PS1+="\[${yello}\]\u";        # yello user name
PS1+="\[${grey}\]@";          # grey '@'
PS1+="\[${maroon}\]\h";       # maroon hostname
PS1+="\[${grey}\]:";          # grey ":"
PS1+="\[${green}\]\W";        # green working dir
PS1+="\[${reset_p}\]\n$ ";    # prompt with the default color
export PS1;

If you put this code in the begining of you .bashrc file, you will get the output as following.


To generate the bash themes, you can use online tools like

You can get the string for PS1 created from one of those sites, put it in the begining of your bashrc and you can use bash with theme that you yourself have created.