Wednesday, April 10, 2013

Colours in your bash shell

Coloring your bash shell can be quite entertaining. Nowadays I think all the distros comes with colors enabled, if not we can enable it with an alias like this one:

$ alias ls='ls -ap --color'

-a shows all the files including the ones starting with a dot '.'
-p shows a slash '/' after the directories
--color enables color :)

I have that alias in my /etc/bash.bashrc (or /etc/bashrc depending on your distro) so every time I start my computer the colors will be there.

Now that we have the colors enabled, we can customize the shell colors using the variable LS_COLORS. 

If you used a Red Hat based distro, you will the file /etc/DIR_COLORS with a configuration example. If not, see below mine's:

COLOR tty
OPTIONS -F -T 0
TERM linux
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM cons25
TERM xterm
TERM rxvt
TERM xterm-color
TERM color-xterm
TERM vt100
TERM dtterm
TERM color_xterm
TERM ansi
TERM screen
TERM screen.linux
TERM kon
TERM kterm
TERM gnome
TERM konsole
EIGHTBIT 1
NORMAL 00       # global default, although everything should be something.
FILE 00         # normal file
DIR 01;34      # directory
LINK 01;36      # symbolic link
FIFO 40;33      # pipe
SOCK 01;35      # socket
BLK 40;33;01    # block device driver
CHR 40;33;01    # character device driver
ORPHAN 01;05;37;41  # orphaned syminks
MISSING 01;05;37;41 # ... and the files they point to
EXEC 01;32
.cmd 01;32 # executables (bright green)
.exe 01;32
.com 01;32
.btm 01;32
.bat 01;32
.sh  01;32
.csh 01;32
.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z   01;31
.Z   01;31
.gz  01;31
.bz2 01;31
.bz  01;31
.tz  01;31
.rpm 01;31
.cpio 01;31
.jpg 01;35 # image formats
.gif 01;35
.bmp 01;35
.xbm 01;35
.xpm 01;35
.png 01;35
.tif 01;35

For a color palette definition, you can check out bash documentation or the manpage for dir_colors. Also, you can use this bash line to show them:

$ for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done

We can choose the colors we want and modify our /etc/DIR_COLORS accordingly. For example, in CentOS we have 01;34 (dark blue) as default directory color, if we want to change it to light blue we change the line:

DIR 01;34 

for
DIR 38;05;75 (following the colors from the previous for loop)
we sabe the file. Now, to generate the variable LS_COLOR we can execute dircolors:

$ dircolors /etc/DIR_COLORS

And export the variable shown. To make it more straight forward we can also do:

$ eval `dircolors /etc/DIR_COLORS`

And that's it!

If you wish to make it effective at boot time, we can add this line to our /etc/bash.bashrc (or /etc/bashrc):

eval `dircolors /etc/DIR_COLORS`

But that will affect the whole system. If we want to affect only our user, we can use the file $HOME/.dir_colors instead (actually any file name would do) and have this in our $HOME/.bashrc:

eval `dircolors /etc/DIR_COLORS`


No comments:

Post a Comment