shell in colours

Sometimes it feels nice to see a bit of colour. One of the most common places is the shell prompt. The concept and usage is the same, whether you want some colour in your prompt or elsewhere.

$ man console_codes
CONSOLE_CODES(4)                                                                           Linux Programmer's Manual

NAME
       console_codes - Linux console escape and control sequences

DESCRIPTION
       The  Linux console implements a large subset of the VT102 and ECMA-48/ISO 6429/ANSI X3.64 terminal controls, plus certain private-mode sequences for changing the color palette, character-set mapping, and so on.  In the tabular descriptions below, the second column gives ECMA-48 or DEC mnemonics (the latter if prefixed with DEC) for the given function. Sequences without a mnemonic are neither ECMA-48 nor VT102.

That was just the bit at the beginning. If you didn't understand, don't let it put you off. The rest of the manual page is much less cryptic.

In short, you need to send control characters to your shell if you want to do anything other than print some character. We precede control characters with an Escape Sequence to differentiate them.

Let's do this with an example. And the most common example is the shell prompt.

In my Gentoo, I get a colour prompt courtesy of the following line in /etc/bash/bashrc.
PS1+='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '

To read this better, we need to identify the control characters. Non-printing escape sequences are enclosed in \[\033[ and m\].

Now we can read the colours as "01;32", "01;34", "00". These are the ECMA-48/ISO 6429/ANSI… sequences for changing attributes like colour, etc. RTFM console_codes for the full list.

00reset all
01bold
31red
32green
33brown
34blue
94light blue

The rest are printable characters - '\u@\h \w $ ' - which show us the familiar prompt
username@hostname working_directory $ 

Now we can read that above PS1 as
- start printing in bold green colour
- print "username@hostname"
- start printing in bold blue colour
- print " working directory $"
- reset bold/colours/etc back to normal

We can use this concept further to colour elsewhere. As these are standards, they are not specific to any particular shell and should be valid everywhere. What might matter is your terminal and/or hardware capabilities.

There are other equivalent syntaxes too. I prefer a shorter version, where colour sequences are enclosed in \e[ and m.
\e[32m\u@\h\e[34m \w $\e[0m

Enclose all non-printing characters within \[ and \].
PS1='\[\e[32m\]\u@\h\[\e[34m\] \w $\[\e[0m\]'

Here I'll show you mine.
if [ ${EUID} -eq 0 ]; then
        PS1='\[\e[31m\]\u@\h\[\e[33m\]:\w \[\e[31m\]#\[\e[0m\] '
else
        PS1='\[\e[32m\]\u@\h\[\e[33m\]:\w \[\e[32m\]$\[\e[0m\] '
fi

I don't use any DM or DE. I login at the console and startx. I did something similar to my boring login screen.

$ cat /etc/issue
\l@\n.\o \m \S{NAME} \s \r \v \U \d \t
# echo -n '\e[32m\l\e[0m@\n.\o \m \S{NAME} \s \e[32m\r\e[0m \v \e[31m\U\e[0m \d \e[32m\t\e[0m >/etc/issue


[1] ANSI Escape Sequences: Colours and Cursor Movement

No comments:

Post a Comment

most viewed