Colorizing the bash shell…
June 28, 2007
Not an app this time around, but still some fun stuff
If you’re like me – colors help to convey information in a very fast manner… when scrolling through log files, directory listings, and such it does help to have a splash of color added.
Here are my bash aliases related to coloring the shell… drop these in your ~/.bashrc if you want them to be added on logon. You can also type them directly at a shell and it will affect only the current shell.
alias ls=”ls –color=auto”
alias less=”less -+C -R”
alias tail=”less +F -R”
Note: the tail alias overrides the actual tail command and uses less in streaming mode to support an updating file with ANSI escape codes. To exit you’ll need to press CTRL-C and then Q. The -+C on less will cause the screen to be cleared between pages (don’t really want that for a less command).
When piping to less or using the aliased tail any ANSI escape codes will be processed and the colors will be changed. The same is true of positioning codes, etc.
If you’re generating files from Perl, the module Term::ANSIColor will help with the insertion of color codes. Just add;
use Term::ANSIColor;
And then you can insert an escape code by calling the ‘color’ function ->
print color(‘bold blue’), “This prints in bold blue!”, “\n”;
print color(‘reset’); # reset the colors before exiting if you want your shell to look normal.
Another thing you can do is colorize the prompt. My prompt came from somewhere on the ‘net (with some minor changes). I just added the following to my .bashrc;
function elite
{
local GRAY=”\[33[1;30m\]“
local LIGHT_GRAY=”\[33[0;37m\]“
local CYAN=”\[33[0;36m\]“
local LIGHT_CYAN=”\[33[1;36m\]“
local NO_COLOUR=”\[33[0m\]“
case $TERM in
xterm*|rxvt*|screen)
local TITLEBAR=’\[33]0;\W:\u@\h:\w07\]’
;;
*)
local TITLEBAR=”"
;;
esac
local temp=$(tty)
local GRAD1=${temp:5}
PS1=”$TITLEBAR\
\n$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\#$GRAY/$CYAN$GRAD1\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H%M)$GRAY/$CYAN\$(date +%d-%b-%y)\
$LIGHT_CYAN)$CYAN-$GRAY-\
$LIGHT_GRAY\n\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\$$GRAY:$CYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY “
PS2=”$LIGHT_CYAN-$CYAN-$GRAY-$NO_COLOUR “
}
elite
This will give you a prompt which looks like this;
--(username@hostname)-(01/pts/8)-(1332/28-Jun-07)--
--($:/tmp/blog/posts)-- _
The cursor will end up where the _ is… there is also a blank line added above the prompt which gives a little spacer between any output and the prompt itself.
There are some good prompts out there, just do a google search for “color bash prompt” to find them
Have fun!