Get the highlights in your inbox every week.
How to change the color of your Linux terminal | Opensource.com
How to change the color of your Linux terminal
Make Linux as colorful (or as monochromatic) as you want.
What's your preferred terminal color palette?
You can add color to your Linux terminal using special ANSI encoding settings, either dynamically in a terminal command or in configuration files, or you can use ready-made themes in your terminal emulator. Either way, the nostalgic green or amber text on a black screen is wholly optional. This article demonstrates how you can make Linux as colorful (or as monochromatic) as you want.
Modern systems usually default to at least xterm-256color, but if you try to add color to your terminal without success, you should check your TERM setting.
Historically, Unix terminals were literally that: physical points at the literal endpoint (termination) of a shared computer system where users could type in commands. They were unique from the teletype machines (which is why we still have /dev/tty devices in Linux today) that were often used to issue commands remotely. Terminals had CRT monitors built-in, so users could sit at a terminal in their office to interact directly with the mainframe. CRT monitors were expensive—both to manufacture and to control; it was easier to have a computer spit out crude ASCII text than to worry about anti-aliasing and other niceties that modern computerists take for granted. However, developments in technology happened fast even then, and it quickly became apparent that as new video display terminals were designed, they needed new capabilities to be available on an optional basis.
For instance, the fancy new VT100 released in 1978 supported ANSI color, so if a user identified the terminal type as vt100, then a computer could deliver color output, while a basic serial device might not have such an option. The same principle applies today, and it's set by the TERM environment variable. You can check your TERM definition with echo:
The obsolete (but still maintained on some systems in the interest of backward compatibility) /etc/termcap file defined the capabilities of terminals and printers. The modern version of that is terminfo, located in either /etc or /usr/share, depending on your distribution. These files list features available in different kinds of terminals, many of which are defined by historical hardware: there are definitions for vt100 through vt220, as well as for modern software emulators like xterm and Xfce. Most software doesn't care what terminal type you're using; in rare instances, you might get a warning or error about an incorrect terminal type when logging into a server that checks for compatible features. If your terminal is set to a profile with very few features, but you know the emulator you use is capable of more, then you can change your setting by defining the TERM environment variable. You can do this by exporting the TERM variable in your ~/.bashrc configuration file:$ echo $TERM
Save the file, and reload your settings:
$ source ~/.bashrc
ANSI color codes
Modern terminals have inherited ANSI escape sequences for "meta" features. These are special sequences of characters that a terminal interprets as actions instead of characters. For instance, this sequence clears the screen up to the next prompt:
$ printf `\033[2J`
It doesn't clear your history; it just clears up the screen in your terminal emulator, so it's a safe and demonstrative ANSI escape sequence.
ANSI also has sequences to set the color of your terminal. For example, typing this code changes the subsequent text to green:
$ printf '\033[32m'
As long as you see color the same way your computer does, you could use color to help you remember what system you're logged into. For example, if you regularly SSH into your server, you can set your server prompt to green to help you differentiate it at a glance from your local prompt. For a green prompt, use the ANSI code for green before your prompt character and end it with the code representing your normal default color:
export PS1=`printf "\033[32m$ \033[39m"`
Foreground and background
You're not limited to setting the color of your text. With ANSI codes, you can control the background color of your text as well as do some rudimentary styling.
For instance, with \033[4m, you can cause text to be underlined, or with \033[5m you can set it to blink. That might seem silly at first—because you're probably not going to set your terminal to underline all text and blink all day—but it can be useful for select functions. For instance, you might set an urgent error produced by a shell script to blink (as an alert for your user), or you might underline a URL.
For your reference, here are the foreground and background color codes. Foreground colors are in the 30 range, while background colors are in the 40 range:
|Fallback to distro's default||\033[39m||\033[49m|
There are some additional colors available for the background:
Setting colors in your terminal session is only temporary and relatively unconditional. Sometimes the effect lasts for a few lines; that's because this method of setting colors relies on a printf statement to set a mode that lasts only until something else overrides it.
The way a terminal emulator typically gets instructions on what colors to use is from the settings of the LS_COLORS environment variable, which is in turn populated by the settings of dircolors. You can view your current settings with an echo statement:
$ echo $LS_COLORS
Or you can use dircolors directly:
$ dircolors --print-database
# image formats
If that looks cryptic, it's because it is. The first digit after a file type is the attribute code, and it has six options:
- 00 none
- 01 bold
- 04 underscore
- 05 blink
- 07 reverse
- 08 concealed
The next digit is the color code in a simplified form. You can get the color code by taking the final digit of the ANSII code (32 for green foreground, 42 for green background; 31 or 41 for red, and so on).
Your distribution probably sets LS_COLORS globally, so all users on your system inherit the same colors. If you want a customized set of colors, you can use dircolors for that. First, generate a local copy of your color settings:
$ dircolors --print-database > ~/.dircolors
Edit your local list as desired. When you're happy with your choices, save the file. Your color settings are just a database and can't be used directly by ls, but you can use dircolors to get shellcode you can use to set LS_COLORS:
$ dircolors --bourne-shell ~/.dircolors
Copy and paste that output into your ~/.bashrc file and reload. Alternatively, you can dump that output straight into your .bashrc file and reload.
$ dircolors --bourne-shell ~/.dircolors >> ~/.bashrc
$ source ~/.bashrc
You can also make Bash resolve .dircolors upon launch instead of doing the conversion manually. Realistically, you're probably not going to change colors often, so this may be overly aggressive, but it's an option if you plan on changing your color scheme a lot. In your .bashrc file, add this rule:
[[ -e $HOME/.dircolors ]] && eval "`dircolors --sh $HOME/.dircolors`"
Should you have a .dircolors file in your home directory, Bash evaluates it upon launch and sets LS_COLORS accordingly.
Colors in your terminal are an easy way to give yourself a quick visual reference for specific information. However, you might not want to lean on them too heavily. After all, colors aren't universal, so if someone else uses your system, they may not see the colors the same way you do. Furthermore, if you use a variety of tools to interact with computers, you might also find that some terminals or remote connections don't provide the colors you expect (or colors at all).
Those warnings aside, colors can be useful and fun in some workflows, so create a .dircolor database and customize it to your heart's content.