Shell – Unix Shell and colours

colorsescape-charactersshellterminal

I need to understand this code snippet that I found in .profile file

echo -en "\e[32;44m $(hostname) \e[m";echo -e "\e[m"

Best Answer

This snippet is used to print out the hostname of the system with a blue background and a green font.

To color your shell, you use special color escape sequences.

\e[ starts the color scheme, 32; will set the foreground color to green, 44 will set the background color to blue and m will end it.

$(command) creates a new shell, executes command and returns the result (not the return value).

hostname returns the hostname of the current system.

\e[m will reset the coloring of the output.

From the echo manpage:

   -n     do not output the trailing newline
   -e     enable interpretation of backslash escapes

IMHO your snippet could be simplified to echo -e "\e[32;44m $(hostname) \e[m";

See the chapter 6.1 Colours of the BASH Prompt HOWTO for more details.

Related Question