Thursday 14 July 2016

Changing color of displayed text in bash shell

So...... I'm feeling a bit colorful & I'd like my shell prompt to reflect on my mood.
I decided to customize the MOTD displayed when I logged in & make it a bit more colorful.

We need to know the color codes for the different colors provided by ANSI escape codes given below:


Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

Then using these codes I added the following lines to my .bash_profile file:

##Custom MOTD for me##

BLUE='\033[0;34m'
LB='\033[1;34m'
NC='\033[0m' # No Color
printf "${LB}=================${NC} \n"
printf "${BLUE}Welcome Sahil${NC} \n"

printf "${LB}=================${NC} \n"


So whenever I login I get this prompt:


On similar lines if we want to modify the shell prompt/PS1 to be of the color green this can be accomplished by adding the following lines in .bash_profile file.:

GREEN='\033[0;32m'
NC='\033[0m' # No Color
PS1="${GREEN}[`id -un`@`hostname | cut -d "." -f 1` ~]#${NC}"

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...