LINUXUSER Command Line

ANSI escape sequences PAINT YOUR BASH Angela Parszyk, Fotolia

This month, we’ll show you how to decorate your console and give it a fresh new look by adding color to the command line with ANSI escape

sequences. BY HEIKE JURZIK

lack on white, or white on black (m) and press the keyboard shortcut program and the virtual console, which – a terminal doesn’t need to be Ctrl+V, Esc (or Ctrl+V, Ctrl+[) in Bash you typically access by pressing Bso boring. Any modern Linux or Ctrl+Q, Esc (or Ctrl+Q, Ctrl+[ or Ctrl+Alt+F1 through Ctrl+Alt+F6) or console can display colors to brighten up Ctrl+Q, 033) in (X)Emacs. an Xterm, the colors will look different program output, welcome messages, and The escape is followed by an and the attributes will have a different even the prompt. This article shows opening square bracket and the color effect on the output. For example, 1 at you how to add a touch of color and dec- code (and closed again by the m charac- the console changes the foreground orate your command line. ter). You can add an optional attribute brightness, whereas it sets the bold or for the foreground color, followed by a normal font aspect in Xterm. Table 1 The Color Palette semicolon (this is 0 for the normal font gives an overview of foreground colors Some programs have their own parame- in this example) and a foreground or along with attributes 0 and 1; Table 2 ters for adding color to output. Both ls background color (this is 34 for blue shows background color options (with- and grep support a --color option that type on a standard background in this out attributes). makes command output easier to read example). The colored text follows (blue To combine foreground and back- (Figure 1). ANSI escape sequences pro- chicken) and ^[[0m switches back to the ground colors – for example, to output vide the underlying mechanism for this. standard settings for the remainder of the text blue chicken on a red back- To get a line of colored output with echo the text. ground – you just add the color code for in Bash, you can add an escape se- Attributes comprise two digits (e.g., the background color to a command for quence to the text (Listing 1). 00). A single digit is sufficient because the foreground color (Listing 1, line 4). The examples show different ap- the first digit is always a zero. Fore- proaches to entering the escape charac- ground attributes include 0, 1 (bold), Color Your Grep ter (^[). The octal notation is \033; if you 4 (underlined), 5 (blinking), or 7 (in- If you don't like the standard color out- use this format (or the \e notation), the verted). The background colors have no put provided by the Grep --color option, Echo program requires the -e option to additional attributes. you can redefine the GREP_COLOR vari- interpret the correctly. As an Depending on whether you are using able with the escape sequences dis- alternative, you can type the character in the console (KDE’s terminal Konsole cussed previously. To replace the default

GLOSSARY Listing 1: Escape Sequence Examples ANSI: The American National Standards 01 echo -e "A \033[0;34mblue chicken\033[0m. Back to normal text." Institute standardizes industrial meth- 02 echo -e "A \e[0;34mblue chicken\e[0m. And back to normal text." ods – control sequences for terminals in 03 echo "A ^[[0;34mblue chicken^[[0m. And back to normal text." this case. Control characters include line breaks and bells, but also colored output 04 echo -e "A \033[0;34m\033[41mblue chicken\033[0m that likes sitting or prompts. on red things."

90 ISSUE 83 OCTOBER 2007 WWW.LINUX- MAGAZINE.COM

0090-091_command.indd90-091_command.indd 9900 008.08.20078.08.2007 19:27:1919:27:19 UUhrhr Command Line LINUXUSER

to the less or more sage of “Debian GNU/Linux 4.0 tty5.” If you take a look at the your way through file, you will see that the host name is the output: dircol- represented by the \n sequence and the ors -p | less. console number is represented by \l. To change the Table 3 shows more options, and the default color set- getty manpage has a complete list. Figure 1: The ls and grep commands will color program output if tings for ls, first To paint your virtual front door, you needed, giving you a clearer overview. create a configura- can use the color codes shown here. An tion file for Dircol- entry of Debian GNU/Linux 4.0 ^[[0;31m highlight red with an underlined tur- ors. The easiest way to do this is to store \n^[[0m \l would display the host name quoise font on a magenta background, the output from dircolors -p in a file and in red, for example. The /etc/motd (mes- just type then edit the file with your favorite text sage of the day) file is displayed when a editor. The dircolors -p > ~/.dircolors user logs on successfully. export GREP_COLOR="04;36;45" command writes the output to a hidden If you are creative, you can use color file called .dircolors in your home direc- codes to paint a picture for your wel- The quotes prevent the shell from inter- tory. Explanations of color codes and file come file. The Linux_Logo [1] program preting the semicolon. The variable is types appear in comment (#) lines. will help you design an attractive logo or only valid in the current shell session. To To change the color for directories banner. Most distributions include pack- recoat Grep permanently, you need to from dark blue to green, for example, lo- ages on the installation medium. As an add the line to your ~/.bashrc file. The cate the line DIR 01;34 # directory and alternative, just visit an RPM search en- next time you log on, the new configura- change the color code to DIR 01;32 # di- gine. The program is easy to use at the tion will be enabled. If you don’t want to rectory for a new color combination. command line; linux_logo on a Debian wait, you can type source ~/.bashrc to Save the file and quit the editor. Now system will paint a neat banner with the reparse the configuration file at run time. type dircolors -b ~/.dircolors to intro- Debian swirl and various information duce Dircolors to the new configuration. about your system. Painting Ls Directories will be shown in new colors Various command-line options are The ls command also uses escape se- the next time you run Ls. To save these available to manipulate the image. For quences to color various file types for settings permanently, either export the example, -a draws in ASCII and - draws listings: directories are blue, executables LS_COLORS variable or add the dircolors a penguin; type -h for a full list. If you are green, and symbolic links are cyan. If command to your ~/.bashrc; this is the like one of these masterpieces, you can you want to change the default Ls colors standard approach on Debian systems, easily set up a new welcome page for the because you can’t read blue text on a for example: shell; for example, black terminal background, for example, the Dircolors program will help you eval "`dircolors -bU # cp /etc/motd /etc/motd.orig modify the LS_COLORS environmental ~/.dircolors`" # linux_logo -b > /etc/motd variable. Of course, you could theoreti- cally set the variable manually; however, Because Dircolors can’t set the variable Users who log on will now be treated to the output from echo $LS_COLORS itself, eval is used to run the command. a colorful welcome message. shows how long and complicated the settings for individual file types are. Dir- Painting Your Front Door Colorful Future colors will handle the task for you and To paint a colorful welcome splash on I still haven’t talked about colorful shell it knows exactly what to paint in which your , you can use escape se- prompts. Read next month’s Command color. Type the name of the executable quences. The welcome message on the Line for more about coloring the prompt, without any arguments to output a list virtual console is governed by the /etc/ using control sequences to position the of the current settings. issue file; the content of the file is dis- cursor, and adding useful data to the Adding the -p switch tells Dircolors to played before the login prompt. On prompt with various other tricks. ■ list the colors used for individual file Debian systems, the file issues a mes- types. It is a good idea to pipe the output Table 3: Control Characters Table 2: Background \d Current date in YYYY-MM-DD format Table 1: Foreground Color code Background \t Current time in HH:MM:SS format Color code 00 01 40 Black \s Operating system name 30 Black Gray 41 Red \m CPU type 31 Red Light red 42 Green \r Kernel number 32 Green Light green 43 Yellow (Xterm)/Brown 33 Brown Yellow (console) 34 Blue Light blue 44 Blue INFO 35 Magenta Pink 45 Magenta [1] Linux_Logo website: 36 Cyan Light cyan 46 Cyan http:// www. deater. net/ weave/ 37 White Light gray 47 Light gray vmwprod/ linux_logo/

WWW.LINUX- MAGAZINE.COM ISSUE 83 OCTOBER 2007 91

0090-091_command.indd90-091_command.indd 9911 008.08.20078.08.2007 19:27:2519:27:25 UUhrhr