Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to work with text colors on Linux

How-To
Dec 18, 20245 mins

Want to add color to the command line on Linux? You can create colored text in your scripts and use color settings to change the background of your screen.

Color palette
Credit: ronstik/Shutterstock

When you list files on the Linux command line, many of the file names show up in colors, as you’ve probably noticed. The colors used depend on the file types. For example, the names of directories will show up in a blue font. Here’s a list of the colors you can expect to see and what they represent:

  • Blue: directory
  • Green: executable or recognized data file
  • Cyan: symbolic link file
  • Yellow with black background: device
  • Magenta (pink): graphic image file
  • Red: archive file
  • Red with black background: broken link
  • Yellow with black background: named pipe

If you’d like to get a quick confirmation on how this works, create an empty directory (e.g., mkdir TEST) and move into it with the cd command. Then run the following commands to create a sampling of the various file types:

mkdir directory
touch script; chmod 750 script
ln -s /tmp symlink
touch image.jpg
touch archive.tgz
mknod named-pipe p
ln -s /tmp/yadda broken_link
mknod named-pipe p
touch file.zip
touch text

Once you run these commands, you will see a colorful listing whenever you run the ls command. The only one of the eight file types mentioned in the bullet list above that will not be set up will be the device file. Run the command “ls /dev/null” to see an example of the colors used for displaying devices.

When you run the ls -l command to get a long listing of the files you just set up in your TEST directory, you will notice that all of the files except two have a file size of 0. This demonstrates that the colors used to display these files are not based on file content but on file names and permissions. The symlink and the broken_link files will have a file size that reflects the length of the file and directory names that they point to, but the content is displayed in the listing itself (e.g., symlink -> /tmp has a size of 4 characters).

Coloring text in your scripts

File listings aren’t the only way to add color to the command line on Linux. You can also create colored text in your scripts. The key to making this easier is having the codes available to display the colors easily accessible. Here’s a list of variables that you could use to give names for a group of colors codes to make the colors easier to use. You might consider putting these lines into a text file that you can source as needed (e.g., with a command like . Colors).

# foreground colors
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
GRAY="\033[90m"
PINK="\033[91m"
NORMAL="\033[0;39m"

To issue a line of text that uses some of these colors, run a command like that shown below.

$ echo -e $GREEN I $YELLOW love $BLUE color $NORMAL
I love color

The -e option with the echo command is described in the man page as “enable interpretation of backslash escapes” but it also allows these color options to work.

How much the letters stand out will, of course, depend on the background color in your window, but you should see the colors selected. The NORMAL setting returns the font color to the default so that you don’t end up displaying all of your text in the last color used.

To avoid having two blank characters in between the words in your phrase, use a command like this instead:

$ echo -e $GREEN I$YELLOW love$BLUE color$NORMAL
I love color

If you want additional colors, you could add these to your Colors file:

LTGREEN="\033[32m"
LTYELLOW="\033[33m"
LTBLUE="\033[34m"
LTMAGENTA="\033[35m"
LTCYAN="\033[36m"
LTWHITE="\033[37m"

NOTE: If ever you mess up and your text colors don’t go back to normal, issue the command below to fix the problem.

$ echo -e $NORMAL

Background colors

You can also use color settings to change the background color of your screen. The command shown below would change your background color after the command is issued, leaving the space above it with the prior background color.

echo -e "\033[45m"
echo -e "\033[102m"

The command below would reset the colors back to their normal setting – the same as using $NORMAL.

echo -e "\033[0;39m"

If you want to add background colors to your Colors file, you could use these entries:

# background colors
BLACKBG="\033[40m"
REDBG="\033[41m"
GREENBG="\033[42m"
YELLOWBG="\033[43m"
BLUEBG="\033[44m"
MAGENTABG="\033[45m"
CYANBG="\033[46m"
WHITEBG="\033[47m"
GRAYBG="\033[100m"
PINKBG="\033[101m"

The command below would issue a “Hope it’s fun!” message using pink letters on a blue background.

echo -e $BLUEBG$PINK"Hope it's fun!"$NORMAL

Wrap-up

Changing font and background colors can make messages stand out and can be entertaining when you want to get a deeper look into how colors work in your terminal window. I hope you enjoy dabbling in colored messages!

sandra_henrystocker

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author