Changing Bash Prompts

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

Getting rid of the directory

Inserting the time

inserting the time in the command prompt

Making the username green

Green Username

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. 

Bash Prompt -- blue time and green username

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.

multicolored command prompt


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.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

Updated on