Wednesday, December 30, 2009

Easily add color to your Bash environment

Posted by Kallin Nagelberg

Whether you want to add some punch to your Bash prompt or emphasize some echo statements in a script, a little bit of color can go a long way. To start off you should download the scripts I have written that demonstrate most of the color options available and provide a handy function to add color to your own scripts. They are available as a TAR or a ZIP, and if you just want to view the source it is available here.

The three files in the archive are:
  • color_vars.sh - colorizing function and color variables
  • echotest.sh - a very simple example of how you could use color_vars to enhance your scripts
  • color_list - prints out a grid showing all the text/background color combinations

Once you've got them extracted you should try running echotest and color_list. echotest should produce output like:


while color_list should produce:



Be sure that your terminal window is large enough to display the grid. In a smallish window things won't line up and I can't take responsibility for what happens :)

Now that you've seen what it can do, it's time for a little explanation of how it works. I'll start by explaining how to use my color function.

Take a look at echotest.sh (it's only a few lines, and I promise nothing too scary). It starts off by sourcing color_vars.sh in order to gain access to the colorizing function as well as all the color identifiers. The next few lines use the color function to set text and background colors, and to reset back to the default terminal color when finished. Calling color with zero arguments resets to default, with one argument you can set the text color, and with two arguments the first is the text color and the second is the background color. All the possible text and background colors are declared at the top of color_vars.sh.

That's all there is to it! This should get you up and running with colorized scripts in no time. If you're interested in how the color function works it is quite simple and you should have little difficulty understanding the function's code.

note: Most modern Bash environments support the same coloring options, though for these examples I've only tested thoroughly using GNU bash v 4.0.33.





Saturday, November 28, 2009

A guide to the ultimate Linux environment

Posted by Kallin Nagelberg

Warning: This blog will contain content that, while not necessarily offensive, will most certainly drive all but the most devout computer users to tears. Turn back now unless you meet the following conditions:

  1. You are engaged with a computer more often than you are with RL.
  2. You see and/or hear code while walking outdoors or when you try and close your eyes.
  3. You think the DRY principle should have been the eleventh commandment.
Still with me? Fantastic. We're in for some good times.

The focus here is going to be pretty specific: Customizing your Bash environment, with an emphasis on the prompt. Anyone working in a Unix based environment with any sort of frequency knows the difference between a good environment and a vanilla environment. Usually the first time you log into a Bash shell you see something like :



Bleh. Servicable, but so bland, uninformative, and quite a waste of space. I've seen better, but I want the best. In fact, every time I see a prompt these days I feel that the delta between what I'm seeing and what I'm dreaming of is really putting a dent in my productivity. In truth it's probably just the dreaming, but I'm not going to let that get in the way. That's why I've begun a quest to create the ultimate Bash prompt, and eventually an entire prompt configuration system.

Bash scripting is something I've always avoided in the past. I was a bit of a late comer to Linux, and only used it when forced to deploy new builds or perform some other sort of server maintenance. After starting a new job recently It's become apparent that this is something I can no longer avoid. I ran into one truly atrocious prompt on a Solaris box, and I said to myself right then and there: "Never again".

So, after a couple days of tinkering around with my .bashrc file I've managed to produce the following:



While far from what I'm aiming for, it's not bad. A few features of note:
  • The prompt spans two lines. One line with some important context information, and the second giving you maximum room to enter your command.
  • The first line automatically adjusts its width to fill the terminal.
  • Some color coding to make things pop.
  • Your PWD, stopped job count, current directory size, and username/hostname.
I've got lots of ideas on how to improve this prompt; from context info such as file count and cpu load, to creating a configuration system, to possibly even animating the prompt.

The code is going to live in a new GitHub repository I just created, so anyone can download it to wherever they're working and, with a couple commands, gain access to the rich feature set I look forward to creating. I hope to tag a version and create a new post detailing this preliminary version, along with installation instructions and possibly some brief tutorial-type stuff explaining how it all works.

I'm open to suggestions for features, so if there's something you'd like to see please do leave a comment! 'Till next time...