Ask Your Question
1

How to set colors for bash shell primary prompt ?

asked 2012-01-06 15:38:15 -0500

BK gravatar image

I want to customise my primary shell prompt.

edit retag flag offensive close merge delete

Comments

1

There are a lot of places where you can find information but I would like to point in the right direction :

Bash Prompt Howto, which goes into greater detail. It is especially helpful for those wanting to understand.

hhlp gravatar imagehhlp ( 2012-01-06 16:04:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2012-01-06 15:43:28 -0500

BK gravatar image

In the Fedora default shell is BASH. Your current prompt setting is stored in PS1 shell variable. There are other variables too, like PS2, PS3 and PS4.

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.

Use echo command to display current BASH prompt:

[test@fedora ~]$ echo $PS1

--

To add colors to the shell prompt use the following command syntax:

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

--

Where,

\e[ Start color scheme

x;y Color pair to use (x;y)

$PS1 is your shell prompt

\e[m Stop color scheme

Let's consider we have two machine "Fedora-1" & "Fedora-2" and to differentiate between these to machine we need following settings:

1] When a user login to Fedora-1 machine the prompt color change to green and

2] When a user login to Fedora-2 machine the prompt color change to red.

To achieve this follow the steps given below:

For Fedora-1, add following line at the end in /etc/bashrc or /etc/profile

[root@fedora-1 ~]# vim /etc/bashrc

PS1="\e[0;32m[\u@\h \W]\$ \e[m "

For Fedora-2, add following line at the end in /etc/bashrc or /etc/profile

[root@fedora-2 ~]# vim /etc/bashrc

PS1="\e[0;31m[\u@\h \W]\$ \e[m "

After making above settings, based on the prompt color users can identify on which machine they are currently login. Fedora-1 machine primary prompt color changed to green color & Fedora-2 machine primary prompt color changed to red color after login to the system.

Note: The above setting is global and it is applied to each user who are login to the system.

To set the prompt color based on user profile then set the PS1 value in ~/.bashrc or ~/.bash_profile file which are located in users home directory.

Eg:

[test@Fedora-1 ~]$ vim /home/test/.bashrc

PS1="\e[0;33m[\u@\h \W]\$ \e[m "

We can also set the BASH primary prompt background color along with prompt color.

Eg;

--

[test@Fedora-1 ~]# PS1="\e[0;30;103m[\u@\h \W]\$ \e[m "

--

Refer the following color coding for shell prompt.

Regular color settings for prompt character:

--

Black='\e[0;30m' # Black

Red='\e[0;31m' # Red

Green='\e[0;32m' # Green

Yellow='\e[0;33m' # Yellow

Blue='\e[0;34m' # Blue

Purple='\e[0;35m' # Purple

Cyan='\e[0;36m' # Cyan

White='\e[0;37m' # White

--

Regular color settings with bold character:

BBlack='\e[1;30m' # Black

BRed='\e[1;31m' # Red

BGreen='\e[1;32m' # Green

BYellow='\e[1;33m' # Yellow

BBlue='\e[1;34m' # Blue

BPurple='\e[1;35m' # Purple

BCyan='\e[1;36m' # Cyan

BWhite='\e[1;37m' # White

--

Regular color settings with underline characters:

UBlack='\e[4;30m' # Black

URed='\e[4;31m' # Red

UGreen='\e[4;32m' # Green

UYellow='\e[4;33m' # Yellow

UBlue='\e[4;34m' # Blue

UPurple='\e ... (more)

edit flag offensive delete link more

Comments

It works. But after using it I've got bugs in terminal (Fedora-20/Gnome-shell). Now I can't delete printed commands using "backspace".

Sempay gravatar imageSempay ( 2014-01-27 14:45:12 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2012-01-06 15:38:15 -0500

Seen: 25,906 times

Last updated: Jan 06 '12