Make a colour BASH prompt

Make a colour BASH prompt

There are two methods I have found to set the BASH prompt: set the PS1 variable or set the PROMPT_COMMAND variable which, in turn, sets PS1. The method I have come to prefer is the latter.

Dynamic Method

Not everyone appreciates a lot of information in their BASH prompt. Some admins find it confusing and prefer the standard prompt or just some minor changes. So I came up with a way to make the prompt adjustable so it can be installed system-wide and configured individually. It dynamically reads the settings every time, so changes made to its configuration do not require it to be reloaded.

The files involved are these:

File name Description Global location Personal location
prompt A tool to change the PS1 prompt. It is sourced rather than run because if run then the settings are lost when the script exits. /usr/local/bin/ $HOME/bin/
z-bash-prompt.sh A script that sets the PS1 prompt to be a dynamic script rather than static string. I named it with a z- prefix so that if it is placed in /etc/profile.d it will be called last. /etc/profile.d/ $HOME/bin/
prompt.bash The script that actually draws the prompt each time. /usr/local/sbin/ $HOME/bin/
colours.sh A file defining ANSI colour codes. Without it you will have a monochrome prompt. /usr/local/bin/ $HOME/bin/

z-bash-prompt.sh

Show/Hide the code for z-bash-prompt.sh

I had originally named the above script 04-bash-prompt.sh, but files named vte*sh in /etc/profile.d/ later appeared that also set PS1 and would override my settings. So I renamed the script to start with “z” so it gets executed last when /etc/profile iterates through all the *.sh files in /etc/profile.d/.

prompt.bash

Show/Hide the code for /etc/profile.d/prompt.bash

colours.sh

Here is an example colours.sh file that you can edit to suit your needs:

Show/Hide the code for colours.sh

prompt

The prompt script to alter the prompt on-demand:

Show/hide /usr/local/bin/prompt script

The above script can be set to be executable and if someone runs it they will get a message telling them to source it instead. If it is sourced without any parameters, it displays the current prompt settings. If you use . prompt help it will give a help message.

.bashrc

If you place the above files in global locations, then all you need in your .bashrc file is:

. /etc/profile

If you place the above in your personal home directory (i.e., only for yourself, not affecting other users) then you can do something like this in your .bashrc:

. $HOME/bin/z-bash-prompt.sh

If you are using a personal $HOME/bin directory then you may wish to add this to your PATH, e.g.:

[[ $PATH =~ $HOME/bin ]] || export PATH=$PATH:$HOME/bin

Aside from that you will put the variables for the prompt settings, but this can be managed using the prompt script, e.g.,:

. prompt all
. prompt save

Controlling the dynamically adjustable prompt

Now that you have placed the above files installed you need only set certain environment variables to control the look of your prompt. For machines with a lot of users this would let you place the prompt settings in /etc/profile.d/ and yet allow each person to control their own prompt as they wish.

You can instantly change your BASH prompt by just setting a variable, e.g, typing the following would immediately cause your prompt to have colour:

BPshowColour=1

The list of variables that control the prompt are in the following table.

Variable Values Meaning
BPshowColour 0=no, 1=yes Use colour in the prompt
BPshowAddresses 0=no, 1=yes, 2=+use Show this machine's IP addresses. If set to 2 it will can also show this machine's purpose, such as Production or Development.
BPshowTime 0=no, 1=yes Show date and time at the right-hand margin
BPshowKernel 0=no, 1=yes Show the kernel version at the right-hand margin
BPshowHistNo 0=no, 1=yes Show the command history number at each prompt
BPshowOS 0=no, 1=yes Show the O/S version, e.g., “Gentoo” or “Oracle Linux”
BPshowUptime 0=no, 1=yes Show up-time
BPshowPath 0=shortest, 1=standard, 2=full Path display can be the shortest possible name (just the last component), the standard name that uses a tilde (~) to represent the HOME directory, or the full path at all times.

To conveniently set those variables and save/update the preferred settings in the user's ~/.bashrc file you can place the following script in the /usr/local/bin/ directory and then just call it with . prompt <setting> any time you want to use it.

I think this is absolutely the best BASH prompt I've ever seen anywhere, and I hope it will be useful to others as well.

Static method

The first method I had presented here set the PS1 prompt and used BASH's built-in dynamic variables, rather than using the PROMPT_COMMAND method above. It is here for reference and for those who might already be using PROMPT_COMMAND for something else, or those who are using this in a Windows BASH emulator that runs the dynamic prompt too slowly (e.g., in MINGW64 I have seen it takes 3~4 seconds to draw the prompt, which ruins the benefit).

Show/Hide static PS1 method

1)
You will probably need to remove any PS1 settings in files such as /etc/bashrc, /etc/bash.bashrc, or /etc/profile since they often set PS1 after the files in /etc/profile.d/ are run.