Changing Bash Prompts
Changing Bash Prompts is fairly straight-forward when you know the commands. The code looks fairly strange when you first look at it, but there is a lot of logic behind it. I used Debian to experiment with how to change the look and feel of the command prompt. Normally you would see something like – user@host:~$ as your command prompt. Tilde (~) being your home directory. If you were in the logs it might look like – user@host: /var/log$ .
Most Linux systems change the command prompt to a pound sign (#) if the root user is signed into the system, instead of the dollar sign ($).
Getting rid of the directory
Inserting the time
Making the username green
Prompt Escape Sequences
Character | Meaning |
\d | Short date format (Tu Jul 24) |
\e | Escape character ( ^[ ) |
\h | Short Host name (Up to the first dot) |
\H | Long Host name |
\j | Number of Jobs running in the shell |
\l | Name of the terminal |
\n | New Line |
\r | Carriage Return |
\s | Name of the shell (ie: BASH) |
\t | Time in a 24-hour format – Hours, Minutes, seconds |
\A | Time in a 24-hour format – Hours, Minutes |
\T | Time in a 12-hour format – Hours, Minutes, seconds |
\@ | Time in a 12-hour format – Hours, Minutes |
\u | Username |
\v | BASH Version (ie: 5.1) |
\V | BASH Version and patch level (ie: 5.1.17) |
\w | The current working directory (ie: /var/log) |
\W | The current working directory restricted to the current folder |
\$ | Shows $ for non root users and # for root users |
\nnn | \033 for escape – ASCII Characters |
\\ | The backslash character itself |
\[ | Escapes the following control characters |
\] | Closes the control character input |
Note: The focus on $PS1 in this article because its siblings are too uncommon in daily work with the shell. There are $PS2, $PS3 and $PS4. These other shells are not normally used, so I will not go into them.
Getting it all right
Once you feel that you have the prompt you want it, is time to write it into your Bashrc file. Whether you decide to go minimalist or extravagant with lots of colors, to keep it current you need to keep it in your local Bashrc file. You can also put a different style in root’s Bashrc file – say green username for the User and a red username for Root. That way you will know which is which just by color coding the usernames.
Scripting them in your Bashrc file – I laid it out so you can play with the colors if you wish:
Here is the one that I scripted.
##############################
LGRAY=”\\033[1;30m”
LRED=”\\033[1;31m”
LGREEN=”\\033[1;32m”
LYELLOW=”\\033[1;33m”
LBLUE=”\\033[1;34m”
LPURPLE=”\\033[1;35m”
LCYAN=”\\033[1;36m”
LWHITE=”\\033[1;37m”
DGRAY=”\\033[0;30m”
DRED=”\\033[0;31m”
DGREEN=”\\033[0;32m”
DYELLOW=”\\033[0;33m”
DBLUE=”\\033[0;34m”
DPURPLE=”\\033[0;35m”
DCYAN=”\\033[0;36m”
DWHITE=”\\033[0;37m”
NOCOLOR=”\\033[0m”PS_LGRAY=”\[${LGRAY}\]”
PS_LRED=”\[${LRED}\]”
PS_LGREEN=”\[${LGREEN}\]”
PS_LYELLOW=”\[${LYELLOW}\]”
PS_LBLUE=”\[${LBLUE}\]”
PS_LPURPLE=”\[${LPURPLE}\]”
PS_LCYAN=”\[${LCYAN}\]”
PS_LWHITE=”\[${LWHITE}\]”
PS_DGRAY=”\[${DGRAY}\]”
PS_DRED=”\[${DRED}\]”
PS_DGREEN=”\[${DGREEN}\]”
PS_DYELLOW=”\[${DYELLOW}\]”
PS_DBLUE=”\[${DBLUE}\]”
PS_DPURPLE=”\[${DPURPLE}\]”
PS_DCYAN=”\[${DCYAN}\]”
PS_DWHITE=”\[${DWHITE}\]”
PS_NOCOLOR=”\[${NOCOLOR}\]”
#############################
# prompt colors
if [ $UID == 0 ]; then
export PS1=”[$ {PS_DBLUE}\T {PS_LRED}\u${PS_NOCOLOR}@${PS_LYELLOW}\H${PS_LBLUE} \W${PS_NOCOLOR}]\$ ”
else
export PS1=”[$ {PS_DBLUE}\T {PS_LGREEN}\u${PS_NOCOLOR}@${PS_LYELLOW}\H${PS_LBLUE} \W${PS_NOCOLOR}]\$ ”
fi
# Add some interactivity
__ps1_cmd_() {
if [ $UID == 0 ] ; then
USER_COLOR=$PS_LRED
else
USER_COLOR=$PS_LGREEN
fi}
PROMPT_COMMAND=__ps1_cmd_
Conclusion
Changing your BASH prompt is fairly easy and now you have the script to place into your Bashrc file. Here it is all zipped up for you in a text file ready to insert into your Bashrc file.