Mar
26
2020 Posted by Damir Wilder

How to put some COLOUR in your BASH or KSH prompt

A recent incident where half of all tablespace containers were accidentally deleted by someone at a client’s site started me thinking: how can I be sure I am running a disruptive database command (such as DROP TABLE) on the right server, i.e. on a TEST and not on the live PRODUCTION?

As I prefer to run my database commands on the command line (I’m not a GUI person), I very soon realised that putting a little colour into the terminal session can make a big difference.

At a first glance, the plain-grey prompts look very much the same (can you spot the difference?):

However, with some colour thrown into it, the difference is obvious:

We humans notice the difference between striking colours much better than between just plain-grey words!

This article explains how putting a few hieroglyphs into your shell environment can make all the difference between a dropped PROD database and a safe TEST experiment.

 

Linux, BASH

If you’re on Linux, the chances are you are using BASH. In that case, go to your home directory (cd ~), open your BASH profile file (vi .bashrc) and add the following commands somewhere at the end of it:

PROD:

TEST:

IMPORTANT: you should use double quotes for the TC_* and PS1 environment variable definitions to enable correct code interpretation by BASH.

Once you have done all that, either restart your terminal session or source the profile by hand (. ~/.bashrc) in order for the changes to take effect.

The explanation of the other abbreviations used in the above PS1 env. variable:

\u = expands into username

\h = expands into hostname (up to 1st dot)

\w = expands into current path (home dir. abbreviated as “~”)

 

AIX, KSH

For the Korn shell (the AIX default) users, similarly, add the following commands to the KSH.profile file:

Note: enter the “^[” escape character manually as CTRL-V,ESC
(simply copy-pasting the above commands will NOT work!)

Again, remember to restart the session or source the profile (. ~/.profile)to activate the changes.

Also, check if the environment variables LOGNAME and HOSTNAME are already defined, if not then define them such as:

(this should also be added to the .profile file, before the PS1 variable definition)

 

Yet more colours (and effects)

You can set up any combination of the foreground and background colours and extra effects (where available) by using the following general syntax:

Where:

Just don’t forget to reset the colour scheme at the end of the prompt (TC_RESET), otherwise your command line may start misbehaving.

Here is an example of a truly colourful prompt (with current date/time included as well):

 

Happy colouring!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

« Previous | Next »
Have a Question?

Get in touch with our expert team and see how we can help with your IT project, call us on +44(0) 870 2411 550 or use our contact form…