Madhav Kobal's Blog

This blog will be dedicated to Linux, Open Source and Technology news, affairs, how-tos and virtually EVERYTHING in these domains.

Bash, in Color

Posted by madhavkobal on 20/08/2009

I find that a little color on the command line helps make things easier to read. ls is easy to colorize: just add the line:

alias ls='ls --color=auto'

in your ~/.bashrc (then source it or open another terminal).

You can also set color up for your manpages. The easiest way to do this is using most as a pager: Install the most package, and then type

export MANPAGER="/usr/bin/most -s"

Try looking at a man page and you’ll suddenly see it in color! However, most isn’t as good as less as a pager, and if you’re accustomed to less you don’t necessarily want to retrain your fingers to new commands. An alternative is to set up assorted termcap-related environment variables to provide colors. Add these lines to your .bashrc:

export LESS_TERMCAP_mb=$'E[01;31m'
export LESS_TERMCAP_md=$'E[01;31m'
export LESS_TERMCAP_me=$'E[0m'
export LESS_TERMCAP_se=$'E[0m'
export LESS_TERMCAP_so=$'E[01;44;33m'
export LESS_TERMCAP_ue=$'E[0m'

Finally, you can install the package grc to provide color for certain commands (check /etc/grc.conf to confirm which ones — you can also add your own definitions here). Use it like this:

grc diff file1 file2 Although in the case of diff, a better solution may be to use vimdiff or gvimdiff, both of which provide colors by default and also puts the files side-by-side in a much easier to read format.

export LESS_TERMCAP_us=$'E[01;32m'

(with thanks to the CLUG wiki, which also has some other tips). Source your ~/.bashrc and type export MANPAGER="less" to clear the most setting (or just open a new terminal window) and take a look at a man page.

Note: not all systems automatically source ~/.bashrc on login. If you’re having problems, trying manually sourcing ~/.bashrc, and if that works, add a line

source ~/.bashrc

to your ~/.bash_profile.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: