Bash - Prompt and Colors

GNU Bash is the default shell on most GNU/Linux distributions. It can be customized in many ways. This time, I want to demonstrate, how you can customize the looks of your Bash and also make it more useful.

Bash - Prompt and Colors

GNU Bash is the default shell on most GNU/Linux distributions. It can be customized in many ways. This time, I want to demonstrate, how you can customize the looks of your Bash and also make it more useful.

This is a follow-up article for the already published "Bash - Tweaks and Tuning" post. Please check it out, if you want to learn more about Bash customizations.

GNU Bash

The GNU Bash page states:

Bash is the GNU Project's shell—the Bourne Again SHell. This is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh).

Hmmm, not very helpful. So what is it now? Let's start with some easy fundamentals. The shell is a program, that can interpret your input in a terminal and send the output back. In the past, this terminal was a hardly wired display and keyboard. Nowadays, we are using virtual terminals.

If you open Putty, GNOME Terminal, Konsole or x-term, you are basically starting a virtual terminal. Every input in this terminal will be sent to the shell - in our case, GNU Bash. This also leads to a situation where these terms are often mixed and represent the same "a black windows with a blinking cursor".

Screenshot - GNOME Terminal

Prompt

The prompt is the little indicator in front of the blinking cursor. Sometimes it shows a name and a host, sometimes it looks a bit more fancy. In this article, we will make the boring prompt above look better and more useful.

Hint
The guide is tested on Fedora 35 with Bash 5.1.8.

For the sake of this tutorial, we will make all adjustments in ~/.bashrc, which applies for the actual user only.

Basics

The GNU Bash prompt is defined in some simple variables. Therefore, testing things out is pretty easy. Just open up your terminal of choice, and we will test some simple adjustments.

You can output the variables for the GNU Bash with some easy commands.

# The default prompt
$ echo $PS1
\u@\h \W]\$

# The prompt for interactive input like passwords
$ echo $PS2
>

# Commands that will be run by GNU Bash
$ echo $PROMPT_COMMAND 
__vte_prompt_command

That's already it. We can change the default prompt by exporting the PS1 variable with some new values. Let's do this in a non-permanent way.

# Change PS1
[dschier@nb01 ~]$ export PS1="myTerm :"
myTerm :

The prompt is changed to myTerm :. That was easy.

But why does our original prompt look different to that indicated by the variable? The answer is easy - GNU Bash has some built-in replacements for certain terms. These are often called escape sequences. You remember the original prompt? It was something like \u@\h \W]$. The \u is short for "current user", the \h will be replaced by the hostname and the \W will be the short version of our current working directory. Lastly, the \$ will be replaced with a "$" for all regular users and with a "#" for the root user.

There is a huge list of possible escape sequences. Below you can find some useful examples.

\a	indicates a visuall bell, which is very helpful for deaf people or 
	if you don't like the "ding", but still get an indicator

\t	indicates the current time in 24h format

\w	the long version of \W

Let's assume, we want to have our prompt showing the time, our user and the long version of the working directory.

# Default Prompt
[dschier@nb01 dschier-wtd]$ 

# Change non persistent
[dschier@nb01 dschier-wtd]$ export PS1="(\t) [\u \w]\$ "

# Resulting Prompt
(18:15:45) [dschier ~/Projects/dschier-wtd]$ 

But... what about persisting the changes? In case you want to use the prompt every time you open your terminal, you just need to add the above command to your ~/.bashrc file.

# Open ~/.bashrc
$ nano ~/.bashrc

You can add it right at the end of the file.

# .bashrc

...SNIP...


# User specific aliases and functions

export PS1="(\t) [\u \w]\$ "
~/.bashrc

This is only the first step to fully customize our prompt.

Colors & Formats

Now that we can adjust the prompt a bit, what about coloring it? If your virtual terminal / terminal emulator supports it, you can adjust the colors of your prompt easily. You just need some special codes, similar to the replacements above.

The below code block demonstrates how this works.

# Pattern

\[\033[FORMAT;COLORm\]	# pattern

# Reset

\[\033[00m\]	# reset / normal font

# Colors

\[\033[30m\]	# black
\[\033[31m\]	# red
\[\033[32m\]	# green
\[\033[33m\]	# yellow
\[\033[34m\]	# blue
\[\033[35m\]	# purple
\[\033[36m\]	# cyan / blue
\[\033[37m\]	# grey / white


# Formats (in combination with red color)

\[\033[0;31m\]	# no format
\[\033[1;31m\]	# bold
\[\033[2;31m\]	# darken
\[\033[3;31m\]	# italic
\[\033[4;31m\]	# underscore
\[\033[5;31m\]	# blink
\[\033[9;31m\]	# strike through

The sequences may look a bit more complicated, but you can do much more with it. You can also use them for bold text, background colors and combinations of these.

Let's use our last example and give it a bit of coloring.

# Command
$ export PS1="(\t) [\u \w]\$ "

# Resulting Prompt
(18:15:45) [dschier ~/Projects/dschier-wtd]$

# Make the time red
$ export PS1="\[\033[31m\](\t)\[\033[00m\] \u \w]\$ "
# or the user green and bold
$ export PS1="(\t) [\[\033[1;32m\]\u\[\033[00m\] \w]\$ "

This may look complicated, but we just added the color for red before the time \t and a reset behind it. This reset is needed at least at the end of the prompt. The result will look something like the below screenshots.

Screenshot - GNOME Terminal

Again, if you want to persist this prompt, just add it to your ~/.bashrc file.

# .bashrc

...SNIP...

# User specific aliases and functions

export PS1="(\t) [\[\033[1;32m\]\u\[\033[00m\] \w]\$ "
~/.bashrc

Just play around, and you will find something that works for you. In case you require some inspiration, you might want to check out the Docs & Links section.

This topic was addressed thousands of times. I have tried to gather some meaningful links that may be helpful or inspiring for you.

Bash Prompt Escape Sequences
Bash/Prompt customization - ArchWiki
How to Customize (and Colorize) Your Bash Prompt
Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.
Customizing Bash - Fedora Magazine
Bash’s environment is designed to be highly customizable. Read on to learn a few quick tips to improve your productivity when using the Bash CLI.

Conclusion

Customization is a wonderful thing, and I played with coloring and customizations quite a lot. I have seen awesome prompts at different occasions like conferences or hackathon and it always a nice opener to ask somebody: "Ouh, your prompt is looking sweet. How have you done it?"

How have you customized your prompt?