How to set colors for bash shell primary prompt ?
I want to customise my primary shell prompt.
I want to customise my primary shell prompt.
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.
[test@fedora ~]$ echo $PS1
--
'\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
--
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
--
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)
Asked: 2012-01-06 15:38:15 -0600
Seen: 25,727 times
Last updated: Jan 06 '12
Bash prompt corruption with Fedora 23 Cloud
How to reset terminal back to show" [user@localhost user]$ " instead of bash-4.2#
bash reverse search (ctrl+r), going forward
Problem with bash in fedora 18
No authentication for single user mode vulnerability is not getting closed in fedora
How to disable SSH root access in Fedora 27 using the Terminal?
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.