A Shell Tricks: Session Color Coding

A Shell Tricks: Session Color Coding
Photo by Patrick Perkins / Unsplash

As most of us, human beings, we learn on our own mistakes and what I learned recently - you better watch the caffeine level in your blood and what terminal window you use to run your commands, especially scalable ones. So, this small how-to is a consequence of  SSH windows mess and brief production outage (yes automation is a two-sided sword and you can recover system as fast as you kill it).

The best way to understand what server you deal with is color-coded command line prompts. Different Shell interpreters and different terminals offer a different commands and capabilities, but modern server-side world is extremely Linux centric, where the BASH dominates the market.

It's actually quite simple especially. BASH uses environment variable PS1 ad prints out it's content at the beginning of the new command line. You can use regular characters, special characters and color indicators.  Plus, there are special symbols to substitute with the BASH and user session parameters. My requirements are quite simple. I want to see the current user, what ansible controller I'm dealing with,  and my ful path location. Ideally, It should not take much from my command line space. Let's take a look at the first version of that:

export PS1="\u@DEV:\W\n\$ "
\u Prompt starts with the current user name. BASH substutes this entry with your active login name.
**@DEV: ** Static string. In fact, host name (\h or \H) would bring more confuion, since all of them named acording to hostname guidelines, and my three envrionemts differs only by few numbers.
\W Full path to the current folder. Your home folder would be abreviated to ~. You also can use \w, but in most cases just folder name is not enough, isn't it?
'\n$ ' Since a path to thr current location could be mouthful, I prefer to start from a new line \n and mark my input with the regular $ sign. Mark that space after a dollar, your input wouldn't "stick" to the prompt and you can read it faster.

That information is not, what I exactly remember by hart, and there are much more control characters you can yous. To save you googling a good one, I put a few links at the bottom of this page. Now, let's add some colors. Since we are deal with the text-based terminals, there are control sequences, that instruct BASH what foreground and background colors should be used.

The simple color instruction looks like: \e[**STL**;**FG**;**BG**m
where:

\e[ Extended istrcution start
STL Font decoration. I'e bold, blinking and so on.
FG Foreground color
BG Background color
m Closing statement

So, for my development environment, green text in the prompt would be not only appropriate, but healthier for your eyes.
Print all in green on the default background: \e[32m
Reset text to the default: \e[m

Let's combine all components and add a small, color-enabled welcome|warning plaque.

export PS1="\[\e[32m\]\u\[\e[m\]@\[\e[32m\]DEV\[\e[m\]: \[\e[36m\]\w\[\e[m\]\n\[\e[37m\]\\$\[\e[m\] "
echo -e "\e[32m###########################################################"
echo -e "##                                                       ##"
echo -e "##   This is a Development Instance                      ##"
echo -e "##   Please watch your steps.                            ##"
echo -e "##                                                       ##"
echo -e "###########################################################\e[m"
PS1 Prompt and welcome plaque

Resulting PS1 prompt contains a lot of the escapes. The reason is - we want control characters out of the string content and each color sequence is surrounded by square brackets.  \[\] . As you may see, brackets are not required for the regular echo command, but you may want to use -e switch, to process those instructions.

I have added mine to the ~/.bash_profile. I have found, thehard way that ~/.bashrc cold not be the right place, or you should test BASH execution mode and run those commands only if it's interactive. The screenshot of the code above shows the final result.

Development server is green but be careful anyways. 

Now you can create a variations for all your environments. I use stop lite patterns: green for DEV/TEST, yellow/orange for STAGE/QA and red for the production tarets.    

 Wrapping up the matter:

  • Use terminal prompts as an additional indication of the current environment. It may save your production environments form downtime or even a destruction.
  • Add the prompt instructions to the .bash_profile to avoid clash with the noninteractive sessions.
  • Use online helpers, to create the first build and learn advanced color codes and additional control characters. See helpful sites below:
  • If you ae looking a functionality beyond omatted prompts - take a look at the modern approach - PROMPT_COMMAND variable.  

BASH Prompt Generators

https://ezprompt.net/ BASH Prompt generator. Interactive site that helps you with PS1 prompt and basic color instructions.
https://robotmoon.com/bash-prompt-generator/ Another BASH prompt enerator, with accent on prompt colors.